[英]Insert Query to insert multiple rows in a table via select and output clause. SQL Server 2008
我已經創建了一個存儲過程(請忽略語法錯誤)
alter proc usp_newServerDetails
(@appid int, @envid int, @serType varchar(20), @servName varchar(20))
as
declare @oTbl_sd table (ID int)
declare @outID1
declare @oTbl_cd table (ID int)
declare @outID2
begin Transaction
insert into server_details(envid, servertype, servername)
output inserted.serverid into @oTbl_sd(ID)
values(@envid, @serType, @servName)
select @outID1 = ID from @oTbl_sd
insert into configdetails(serverid, servertype, configpath, configtype)
output inserted.configid into @oTbl_cd(ID)
(select @outID1, cm.servertype, cm.configpath, cm.configtype
from configpthmaster cm
where cm.appid = @appid )
select @outID2 = ID from @oTbl_cd
insert into configkeydetails(confiid, keyname)
output inserted.Keyid into @oTbl_ckd(ID)
(select @outID2, cm.key
from configpthmaster cm
where cm.appid = @appid)
begin
commit
end
server_details
表具有一個標識列ID
,該標識列ID
是自動生成的。 @outID1
和第一次插入查詢僅插入1行。
configpthmaster
表與其他任何表都不直接相關,並且具有2個唯一的數據行,我想提取這些行以將數據插入到其他表中,並在插入過程中一個接一個地插入。
第二個插入查詢從configpthmaster
表中獲取數據,並在生成(自動生成的)ID時在configdetails
插入2行。 @outID2
。
它還具有映射到server_details
的FK。
問題是“ @ outID2”僅給出最后插入的ID(即,如果兩個ID生成100,101,我得到101),最終在第3次插入時,僅插入具有相同ID 101的2行,但我希望插入應該是線性的。 即一個代表100,另一個代表101。
如果插入時受影響的零行如何回滾事務?
如何達到這些要求? 請幫忙。
您可以嘗試這種解決方案嗎?
alter proc usp_newServerDetails(@appid int, @envid int,@serType varchar(20),@servName varchar(20))
as
declare @oTbl_sd table (ID int)
declare @outID1
declare @oTbl_cd table (ID int)
declare @outID2
begin Transaction
insert into server_detials(envid,servertype,servername)
output inserted.serverid into @oTbl_sd(ID)
values(@envid ,@serType ,@servName)
select @outID1 = ID from @oTbl_sd
insert into configdetails(serverid,servertype,configpath,configtype)
output inserted.configid into @oTbl_cd(ID)
(select @outID1 ,cm.servertype,cm.configpath,cm.configtype from configpthmaster cm where cm.appid = @appid )
select @outID2 = ID from @oTbl_cd
insert into configkeydetails(confiid,keyname)
output inserted.Keyid into @oTbl_ckd(ID)
(select isnull(replace(stuff((SELECT inserted.configid FOR xml path('')), 1, 1, ''), '&', '&'), '') ,cm.key, from configpthmaster cm where cm.appid = @appid )
begin
commit
end
我剛剛在您的代碼中添加了STUFF。
STUFF函數將一個字符串插入另一個字符串。
請注意,使用STUFF會極大地減慢代碼的處理時間。
有關STUFF的更多信息
如下更改您的程序,然后重試。
ALTER PROCEDURE usp_newServerDetails(@appid int, @envid int,@serType varchar(20),@servName varchar(20))
AS
BEGIN
BEGIN TRY
DECLARE @Output TABLE (ID int,TableName VARCHAR(50),cmKey VARCHAR(50)) --table variable for keeping Inserted ID's
BEGIN TRAN
IF EXISTS ( SELECT 1 FROM configpthmaster cm WHERE cm.appid = @appid )
AND ( SELECT 1 FROM configkeydetails ck WHERE ck.appid = @appid ) --add a conditon to satisfy the valid insertions
BEGIN
INSERT INTO server_detials(envid,servertype,servername)
OUTPUT inserted.serverid,'server_detials',NULL INTO @Output(ID,TableName,cmKey )
VALUES(@envid ,@serType ,@servName)
INSERT INTO configdetails(serverid,servertype,configpath,configtype)
OUTPUT inserted.configid,'configdetails',cm.Key INTO @Output(ID,TableName,cmKey )
SELECT t.ID,cm.servertype,cm.configpath,cm.configtype
FROM configpthmaster cm
CROSS APPLY (SELECT ID FROM @Output WHERE TableName='server_detials')t
WHERE cm.appid = @appid
INSERT INTO configkeydetails(configId,keyname)
SELECT ID,cmKey FROM @Output
WHERE TableName='configdetails'
END
COMMIT TRAN
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK
END CATCH
END
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.