Sql server 在SQL Server 2008中将批处理行插入到两个表中

Sql server 在SQL Server 2008中将批处理行插入到两个表中,sql-server,Sql Server,我需要在表1中插入多行,同时在表2中插入一行,其中包含来自表1的pkID和来自SP参数的值 我创建了一个存储过程,该过程使用表值参数执行批插入,该参数包含要插入到表1中的行。但我在将行插入表2中时遇到了一个问题,该行带有表1中相应的Id标识以及我传递的参数值 是否有人实施了这一点,或者有什么好的解决方案 CREATE PROCEDURE [dbo].[oSP_TV_Insert] @uID int ,@IsActive int ,@Type int -- i need to inse

我需要在表1中插入多行,同时在表2中插入一行,其中包含来自表1的pkID和来自SP参数的值

我创建了一个存储过程,该过程使用表值参数执行批插入,该参数包含要插入到表1中的行。但我在将行插入表2中时遇到了一个问题,该行带有表1中相应的Id标识以及我传递的参数值

是否有人实施了这一点,或者有什么好的解决方案

CREATE PROCEDURE [dbo].[oSP_TV_Insert]
  @uID int
  ,@IsActive int
  ,@Type int -- i need to insert this in table 2
  ,@dTableGroup table1  READONLY -- this one is a table valued
AS

DECLARE @SQL varchar(2000)
DECLARE @table1Id int
 BEGIN

    INSERT INTO dbo.table1  
        (uID
        ,Name
        ,Contact
        ,Address
        ,City
        ,State
        ,Zip
        ,Phone
        ,Active)  
    SELECT 
        @uID  
        ,Name
        ,Contact
        ,Address
        ,City
        ,State
        ,Zip
        ,Phone
        ,Active
        ,@G_Active   
    FROM @dTableGroup
--the above query will perform batch insert using the records from dTableGroup which is table valued

SET @table1ID = SCOPE_IDENTITY()

-- this below will perform inserting records to table2 with every Id inserted in table1.

Insert into table2(@table1ID , @type)

由于需要所有插入的标识值,请查看insert语句的output子句:

由于需要所有插入的标识值,请查看insert语句的output子句:

您需要临时存储插入的标识值,然后使用output子句创建第二条insert语句

比如:

-- declare table variable to hold the ID's that are being inserted
DECLARE @InsertedIDs TABLE (ID INT)

-- insert values into table1 - output the inserted ID's into @InsertedIDs
INSERT INTO dbo.table1(ID, Name, Contact, Address, City, State, Zip, Phone, Active)  
     OUTPUT INSERTED.ID INTO @InsertedIDs
     SELECT 
         @ID, Name, Contact, Address, City, State, Zip, Phone, Active, @G_Active   
    FROM @dTableGroup
然后您可以得到第二条INSERT语句:

INSERT INTO dbo.table2(Table1ID, Type)
    SELECT ID, @type FROM @InsertedIDs

有关如何使用OUTPUT子句的更多详细信息,请参阅。OUTPUT子句是SQL Server目前最未充分利用和最未知的功能之一

您需要临时存储插入的标识值,然后使用OUTPUT子句创建第二条INSERT语句

比如:

-- declare table variable to hold the ID's that are being inserted
DECLARE @InsertedIDs TABLE (ID INT)

-- insert values into table1 - output the inserted ID's into @InsertedIDs
INSERT INTO dbo.table1(ID, Name, Contact, Address, City, State, Zip, Phone, Active)  
     OUTPUT INSERTED.ID INTO @InsertedIDs
     SELECT 
         @ID, Name, Contact, Address, City, State, Zip, Phone, Active, @G_Active   
    FROM @dTableGroup
然后您可以得到第二条INSERT语句:

INSERT INTO dbo.table2(Table1ID, Type)
    SELECT ID, @type FROM @InsertedIDs

有关如何使用OUTPUT子句的更多详细信息,请参阅。OUTPUT子句是SQL Server目前最未充分利用和最未知的功能之一

你能发布代码和你认为不正常的东西吗?如果您在获取插入记录的标识值时遇到问题,INSERT有一个OUTPUT子句将为您提供该值。如果我正确理解了您的意思,并且您在2008年看到了,您可以发布代码吗?您认为哪些代码工作不正常?如果您在获取插入记录的标识值时遇到问题,INSERT有一个OUTPUT子句将为您提供该值。如果我正确理解了您的意思,并且您在2008年看到了,那么我认为这里获得的scope_标识是最后一个?第一个呢?您的标识列的名称是什么?它看起来不是ID,因为您在存储过程insert语句中提供了一个值。我想这里得到的scope_标识是最后一个?第一个呢?标识列的名称是什么?它看起来不像是ID,因为您在存储过程insert语句中提供了一个值。