关联多个表
我的问题是:
我有三个表....前两个表的 id
为IDENTITY
...我需要获取第二个表中第一个表的id
然后我需要获取第三个表中第一个和第二个表的id
表.....我能够将第一个表的id
获取到第二个表中,
但是我无法将第二个表的id
获取到第三个表中... ........我正在使用 WCF SQL 适配器来使用存储过程并我的存储过程看起来像这样
CREATE Procedure [dbo].[InsertHeader]
(
@parHeader As Header READONLY,
@parHeader_Details As HeaderDetails READONLY,
@parHeader_Details1 As HeaderDetails1 READONLY
)
AS
SET NOCOUNT ON
BEGIN
DECLARE @id int, @id1 int
INSERT INTO EDI834_5010_Header([File_Name_EDI834], [ST01], [ST02], [ST03],
[BGN01__TransactionSetPurposeCode],
[BGN02__TransactionSetIdentifierCode],
[BGN03__TransactionSetCreationDate],
[BGN04__TransactionSetCreationTime],
[BGN08__ActionCode], [SE01], [SE02])
SELECT
[File_Name_EDI834], [ST01], [ST02], [ST03],
[BGN01__TransactionSetPurposeCode], [BGN02__TransactionSetIdentifierCode],
[BGN03__TransactionSetCreationDate], [BGN04__TransactionSetCreationTime],
[BGN08__ActionCode], [SE01], [SE02]
FROM @parHeader;
SET @id = @@IDENTITY;
INSERT INTO EDI834_5010_2000([Header_Id], [INS01__InsuredIndicator],
[INS02__IndividualRelationshipCode],
[INS03__MaintenanceTypeCode],
[INS04__MaintenanceReasonCode],
[INS05__BenefitStatusCode])
SELECT @id, [INS01__InsuredIndicator], [INS02__IndividualRelationshipCode],
[INS03__MaintenanceTypeCode], [INS04__MaintenanceReasonCode],
[INS05__BenefitStatusCode]
FROM @parHeader_Details;
SET @id1 = @@IDENTITY;
INSERT INTO EDI834_5010_2300Loop([Id_Header_Id], [Id_Loop2000],
[HD01_MaintenanceTypeCode], [HD03_InsuranceLineCode],
[HD04_PlanCoverageDescription])
SELECT @id, @id1,
HD01_MaintenanceTypeCode, HD03_InsuranceLineCode,
HD04_PlanCoverageDescription
FROM @parHeader_Details1;
RETURN @id1;
END
我需要在存储过程中更改什么才能将第二个表的 id 获取到第三个表....... xml 中有很多循环,所以我需要在第三个表中获取适当的 ID
我的数据如下所示
<Header details>
<Header_Details1> data </Header_Details>
<Header_Details1> data </Header_Details>
<Header_Details1> data </Header_Details>
<Header_details>
<Header details>
<Header_Details1> data </Header_Details>
<Header_Details1> data </Header_Details>
<Header_Details1> data </Header_Details>
<Header_details>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,你的主要问题是:你不只是插入一行 - 而是一大堆行!
因此,虽然您可以使用
@@IDENTITY
来获取最后插入的标识值(顺便说一句:我建议使用SCOPE_IDENTITY()
- 请参阅此处了解原因) - 这仅适用于单行!您需要的是一种输出多个插入身份的机制 - 使用 OUTPUT 子句:
对于第一个语句,这会将所有插入的标识值输出到表变量
@InsertedIDs
中。现在,对于您的第二个表 - 是否有任何列可以将第一个和第二个插入的 ID 关联起来?您需要将第二个
INSERT
中插入的标识值捕获到同一个表变量中,但您需要以某种方式知道哪个ID1
与ID2< /code> - 坦率地说,我不知道如何在您的语句中完成......
但最终,您将拥有一个包含 n 行
(ID1, ID2) 然后您可以使用它插入到第三个表中。
Well, your main problem is: you're not just inserting a single row - but a whole bunch of rows!
So while you can use
@@IDENTITY
to get the last inserted identity value (btw: I would recommend usingSCOPE_IDENTITY()
instead - see here as for why) - this will only work for a single row!What you need is a mechanism to output multiple inserted identities - use the OUTPUT clause:
For the first statement, this will output all inserted identity values into the table variable
@InsertedIDs
.Now for your second table - is there any column that you can relate the first and second inserted ID's by?? You would need to capture the inserted identity values from the second
INSERT
into the same table variable, but you need to somehow know whichID1
to associate with withID2
- and I quite frankly don't see how that would be done in your statements.....But in the end, you would have a table variable with n rows of
(ID1, ID2)
which you can then use to insert into your third table.