2011-09-02 96 views
1

我有一個要求將多行插入到table1中,並同時將一行插入table2,pkIDtable1和一個來自SP參數的值。將批量行插入到SQL Server 2008中的兩個表中

我創建了一個存儲過程,該存儲過程使用包含要插入到table1中的行的表值參數執行批量插入。但我有一個問題,將table2中的行插入到中,其中有與table1對應的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) 
+0

你能後的代碼,你認爲什麼是不正常?如果您在獲取插入記錄的標識值方面存在問題,那麼'INSERT'具有'OUTPUT'子句,它將爲您提供該值。 – a1ex07

+1

如果我已經正確理解你並且你在2008年看到[使用merge..output獲取source.id和target.id之間的映射](http://stackoverflow.com/questions/5365629/using-merge-output- to-get-mapping-between-source-id-and-target-id) –

+0

是的,馬丁,你是對的。 – user335160

回答

4

您需要臨時存儲的插入的標識值,然後創建第二個INSERT語句 - 使用OUTPUT條款。

喜歡的東西:

-- 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子句做更多詳情請參見MSDN docs on the OUTPUT clause - 的一個最近使用不足和SQL Server最「未知」的功能!

+0

marc_s-非常感謝你。它的工作原理:) :) – user335160

1

使用OUTPUT子句,只有一個語句用於插入數據兩個目標表另一種方法:

--Parameters 
DECLARE @TableGroup TABLE 
(
    Name NVARCHAR(100) NOT NULL 
    ,Phone VARCHAR(10) NOT NULL 
); 
DECLARE @Type INT; 
--End Of parameters 

--Destination tables 
DECLARE @FirstDestinationTable TABLE 
(
    FirstDestinationTableID INT IDENTITY(1,1) PRIMARY KEY 
    ,Name NVARCHAR(100) NOT NULL 
    ,Phone VARCHAR(10) NOT NULL 
); 
DECLARE @SecondDestinationTable TABLE 
(
    SecondDestinationTable INT IDENTITY(2,2) PRIMARY KEY 
    ,FirstDestinationTableID INT NOT NULL 
    ,[Type] INT NOT NULL 
    ,CHECK([Type] > 0) 
); 
--End of destination tables 

--Test1 
--initialization 
INSERT @TableGroup 
VALUES ('Bogdan SAHLEAN', '0721200300') 
     ,('Ion Ionescu', '0211002003') 
     ,('Vasile Vasilescu', '0745600800'); 
SET  @Type = 9; 

--execution 
INSERT @SecondDestinationTable (FirstDestinationTableID, [Type]) 
SELECT FirstINS.FirstDestinationTableID, @Type 
FROM 
(
INSERT @FirstDestinationTable (Name, Phone) 
OUTPUT inserted.FirstDestinationTableID 
SELECT tg.Name, tg.Phone 
FROM @TableGroup tg 
) FirstINS 

--check records 
SELECT * 
FROM @FirstDestinationTable; 
SELECT * 
FROM @SecondDestinationTable; 
--End of test1 

--Test2 
--initialization 
DELETE @TableGroup; 
DELETE @FirstDestinationTable; 
DELETE @SecondDestinationTable; 

INSERT @TableGroup 
VALUES ('Ion Ionescu', '0210000000') 
     ,('Vasile Vasilescu', '0745000000'); 
SET  @Type = 0; --Wrong value 

--execution 
INSERT @SecondDestinationTable (FirstDestinationTableID, [Type]) 
SELECT FirstINS.FirstDestinationTableID, @Type 
FROM 
(
INSERT @FirstDestinationTable (Name, Phone) 
OUTPUT inserted.FirstDestinationTableID 
SELECT tg.Name, tg.Phone 
FROM @TableGroup tg 
) FirstINS 

--check records 
DECLARE @rc1 INT, @rc2 INT; 
SELECT * 
FROM @FirstDestinationTable; 
SET  @rc1 = @@ROWCOUNT; 
SELECT * 
FROM @SecondDestinationTable; 
SET  @rc2 = @@ROWCOUNT; 
RAISERROR('[Test2 results] @FirstDestinationTable: %d rows; @@SecondDestinationTable: %d rows;',1,1,@rc1,@rc2); 
--End of test1 
+0

+1可組合的DML僅在SQL Server 2008上可用,但有很多限制。 –