2013-10-17 164 views
2

如何在同一時間將多行插入到兩個不同的表中。選擇行並將行插入到多個表中,sql server

我有3個表

create table temp_source 
(
    GroupID varchar(10) primary key, 
    ExMsg varchar(20) 
); 


create table temp_exceptions 
(
    ExID int identity primary key, 
    ExMsg varchar(20) 
); 

create table temp_sjobs 
(
    GroupID varchar(10) primary key, 
    ExID int 
); 

temp_sjobs.ExIDtemp_exceptions.ExID

現在我想一切從temp_source表中的行插入temp_sjobstemp_exceptions表在同一時間一個外鍵關係。

我能做到這一點的唯一方法是通過在temp_source表的每一行循環,將該行插入temp_exceptions表,拿到@ExID = scope_identity()並插入一行到 temp_sjobs表。

這看起來很慢,因爲循環查詢需要很多時間。

有沒有一種更好的方法來同時插入到多個表中。

+0

爲什麼不先插入異常表然後再輸入soure表? –

+0

那就是我在做什麼,但是我必須循環遍歷每一行。 –

+0

這不是一個單一的工作嗎?如果有幫助,您必須刪除批量插入的外鍵並再次添加。 – Kaf

回答

0

當然,你可以做以下的 - 可能是最簡單的方法:

SELECT INTO temp_sjobs FROM temp_source 
SELECT INTO temp_exceptions FROM temp_source 

如果你想要做這一切的同時,你可以以此爲CTECURSOR。除此之外,你就必須做到這一點作爲一個TRIGGER - 基本上,如果有在temp_source一個新的記錄,那麼它將INSERT新記錄插入temp_sjobstemp_exceptions

+0

這不起作用,因爲它不會在temp_sjobs和temp_exceptions之間創建外鍵關係。它也不考慮列數據類型。 – axblount

0

我認爲最好的方法是使用兩個INSERT語句

INSERT INTO temp_exception (ExMsg) 
SELECT DISTINCT ExMsg 
    FROM temp_source 

INSERT INTO temp_sjobs (GroupID, ExID) 
SELECT ts.GroupID, te.ExID 
    FROM temp_exceptions te 
    JOIN temp_source ts 
    ON ts.ExMsg = te.ExMsg 

首先建立temp_exception表,然後創建temp_sjobs表和鏈接兩個在一起。

+0

這不會工作,因爲ExMsg不是唯一的。 –

+0

@SoumyaK我只是做了小編輯('SELECT DISTINCT')。如果我們確定'ExMsg'在'temp_exception'表中是唯一的,它會起作用嗎? – axblount

+0

不,我只想將插入的異常(temp_exceptions)的相應標識添加到第二個表(temp_sjobs) –

1

使用OUTPUT子句可以幫助你的情況,請找樣本查詢

DECLARE @temp_source table 
(
GroupID varchar(10) primary key, 
ExMsg varchar(20) 
); 

DECLARE @temp_exceptions table 
(
ExID int identity primary key, 
ExMsg varchar(20) 
); 


INSERT INTO @temp_source Select 1,'message1' 
INSERT INTO @temp_source Select 2,'message2' 
INSERT INTO @temp_source Select 3,'message3' 
INSERT INTO @temp_source Select 4,'message4' 


DECLARE @temp_sjobs table 
(
GroupID varchar(10) primary key, 
ExID int 
); 


DECLARE @temp_InsertedExceptionOutput table 
(
    ExID Int, 
    ExMsg varchar(20) 
) 

INSERT INTO @temp_exceptions (ExMsg) 
OUTPUT inserted.ExID, inserted.ExMsg into @temp_InsertedExceptionOutput 
Select ExMsg from @temp_source 

INSERT INTO @temp_sjobs(GroupID,ExID) 
SELECT ts.GroupID, eo.ExID 
FROM @temp_InsertedExceptionOutput eo 
JOIN @temp_source ts on ts.ExMsg = eo.ExMsg 


Select * from @temp_source 
Select * from @temp_exceptions 
Select * from @temp_sjobs 

你所擁有的表的設計下面我假設ExMsg是唯一否則你可能會發現完整性問題。

0

正如我所瞭解的,你想要將你的表temp_source拆分成temp_exceptions和temp_sjobs,並且在它們之間有一個外鍵關係。我也遇到過相同的情況,並且我選擇了像下面這樣的解決方案以獲得更好的性能:

declare @Exid int 
select @Exid = max(ExID) from temp_exceptions 

if @Exid is null 
set @Exid = 0 

set Identity_insert temp_exceptions ON 

insert into temp_exceptions (ExID, ExMsg) 
select @Exid + row_number() over(order by (select NULL)), ExMsg from temp_source 

insert into temp_sjobs (ExID, GroupID) 
select @Exid + row_number() over(order by (select NULL)), GroupID from temp_source 

set Identity_insert temp_exceptions OFF 

select * from temp_exceptions; 
select * from temp_sjobs;