关联多个表

发布于 2024-12-22 01:35:29 字数 3257 浏览 2 评论 0 原文

我试图扩展上面的链接 http://blogs.msdn.com/b/biztalkcpr/archive/2009/10/05/inserting-parent-child-records-with-identity-column-using-wcf-sql-adapter- in-one-transaction.aspx#comments

我的问题是:

我有三个表....前两个表的 idIDENTITY...我需要获取第二个表中第一个表的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>

I was trying to extend the above link http://blogs.msdn.com/b/biztalkcpr/archive/2009/10/05/inserting-parent-child-records-with-identity-column-using-wcf-sql-adapter-in-one-transaction.aspx#comments

My problem is :

I have three table....the first two tables have id as IDENTITY... I need to get the id of first table in 2nd table

Then I need to get id of 1st and 2nd table in 3rd table ..... I was able to get id of first table into 2nd table

But I'm not able to get id of 2nd table into 3rd table...........I am using WCF SQL adapter to consume the stored procedure and my stored procedure looks like this

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

What do I need to change in my stored procedure to get the id of 2nd table into 3rd....... there are so many looping in xml so I need to get the appropriate ids in 3rd table

And my data looks like this

 <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

々眼睛长脚气 2024-12-29 01:35:29

好吧,你的主要问题是:你不只是插入一行 - 而是一大堆行!

因此,虽然您可以使用 @@IDENTITY 来获取最后插入的标识值(顺便说一句:我建议使用 SCOPE_IDENTITY() - 请参阅此处了解原因) - 这适用于单行!

您需要的是一种输出多个插入身份的机制 - 使用 OUTPUT 子句

DECLARE @InsertedIDs TABLE (ID1 INT, ID2 INT)

INSERT INTO EDI834_5010_Header(......)
OUTPUT Inserted.ID INTO @InsertedIDs(ID1) 
SELECT  
   ....... 
FROM @parHeader;

对于第一个语句,这会将所有插入的标识值输出到表变量@InsertedIDs中。

现在,对于您的第二个表 - 是否有任何列可以将第一个和第二个插入的 ID 关联起来?您需要将第二个 INSERT 中插入的标识值捕获到同一个表变量中,但您需要以某种方式知道哪个 ID1ID2< /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 using SCOPE_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:

DECLARE @InsertedIDs TABLE (ID1 INT, ID2 INT)

INSERT INTO EDI834_5010_Header(......)
OUTPUT Inserted.ID INTO @InsertedIDs(ID1) 
SELECT  
   ....... 
FROM @parHeader;

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 which ID1 to associate with with ID2 - 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文