2016-09-13 57 views
1

我有下面的3個表「#tblData」,「#tblMaster」和「#tblChild」,我需要將「#tblData」分成兩個其他表「#tblMaster」和「 #tblChild」。來自單一來源表的主子表項

在「#tblChild」表中,我需要來自「#tblMaster」的身份密鑰以及來自「#tblData」表的其他數據。

Create Table #tblData (Id int, UniqueKey VARCHAR(10), DateTimeNow DATETIME, UpdateBy VARCHAR(10)) 
insert into #tblData values (1, 'uq1', getdate(), 'abc'), (2, 'uq2', getdate()+1, 'xyz') 
--select * from #tblData 
create Table #tblMaster (MasterId INT IDENTITY(100,1), DateTimeNow DATETIME, UpdateBy VARCHAR(10)) 
create Table #tblChild (ChildId INT IDENTITY(10,1), MasterId INT, UniqueKey VARCHAR(10), DateTimeNow DATETIME) 

我試圖用MERG,但不能從「#tblMaster」到#tblChild」插入標識值。

下面光標解決我的問題,而是尋找更好的替代品,

Create Table #tblData (Id int, UniqueKey VARCHAR(10), DateTimeNow DATETIME, UpdateBy VARCHAR(10)) 
insert into #tblData values (1, 'uq1', getdate(), 'abc'), (2, 'uq2', getdate()+1, 'xyz') 
--select * from #tblData 
    create Table #tblMaster (MasterId INT IDENTITY(100,1), DateTimeNow DATETIME, UpdateBy VARCHAR(10)) 
create Table #tblChild (ChildId INT IDENTITY(10,1), MasterId INT, UniqueKey VARCHAR(10), DateTimeNow DATETIME) 

declare @id int 
DECLARE @MyCursor CURSOR 
SET @MyCursor = CURSOR FAST_FORWARD FOR 
SELECT Id FROM #tblData 
OPEN @MyCursor 
FETCH NEXT FROM @MyCursor INTO @id 
WHILE @@FETCH_STATUS = 0 
BEGIN 
declare @UniqueKey VARCHAR(10) declare @DateTimeNow datetime 
declare @iIdentity int 
create table #OutputTbl (Id Int, UniqueKey VARCHAR(10), DateTimeNow DATETIME) 

select @UniqueKey = UniqueKey, @DateTimeNow = DateTimeNow from #tblData where Id = @id 

INSERT INTO #tblMaster(DateTimeNow, UpdateBy) SELECT DateTimeNow, UpdateBy FROM #tblData where Id = @id 
SELECT @iIdentity=SCOPE_IDENTITY() 

INSERT INTO #OutputTbl values (@iIdentity, @UniqueKey, @DateTimeNow) 
select * from #OutputTbl 
drop table #OutputTbl 

FETCH NEXT FROM @MyCursor INTO @id 
END 
CLOSE @MyCursor 
DEALLOCATE @MyCursor 

drop table #tblChild 
drop table #tblMaster 
drop table #tblData 
+2

標籤您正在使用的數據庫管理系統。很多特定於產品的SQL ... – jarlh

+0

問題中的查詢看起來像SQL Server。如果我錯了,請編輯標籤。 –

回答

0

在這種簡單的情況下,不需要光標。

您可以使用MERGEOUTPUT子句。 MERGE中的OUTPUT子句允許從源表和目標表中訪問列。

的樣本數據

Create Table #tblData (Id int, UniqueKey VARCHAR(10), DateTimeNow DATETIME, UpdateBy VARCHAR(10)); 
insert into #tblData values (1, 'uq1', getdate(), 'abc'), (2, 'uq2', getdate()+1, 'xyz'); 

create Table #tblMaster (MasterId INT IDENTITY(100,1), DateTimeNow DATETIME, UpdateBy VARCHAR(10)); 
create Table #tblChild (ChildId INT IDENTITY(10,1), MasterId INT, UniqueKey VARCHAR(10), DateTimeNow DATETIME); 

查詢

MERGE可以插入,更新和刪除,但我們只需要簡單的插入,所以連接標準始終爲假(1=0)。

MERGE INTO #tblMaster AS Dest 
USING 
(
    SELECT UniqueKey, DateTimeNow, UpdateBy 
    FROM #tblData 
) AS Src 
ON (1 = 0) 
WHEN NOT MATCHED BY TARGET THEN 
INSERT 
    (DateTimeNow 
    ,UpdateBy) 
VALUES 
    (Src.DateTimeNow 
    ,Src.UpdateBy) 
OUTPUT inserted.MasterId, Src.UniqueKey, Src.DateTimeNow 
INTO #tblChild(MasterId, UniqueKey, DateTimeNow) 
; 

檢查和清理

select * from #tblData; 
select * from #tblMaster; 
select * from #tblChild; 

DROP TABLE #tblData; 
DROP TABLE #tblMaster; 
DROP TABLE #tblChild; 
+0

驚人的合併,謝謝! – user584018

0

您需要將列添加到您的#tblMasterId#tblData

Create Table #tblData (Id int, UniqueKey VARCHAR(10), DateTimeNow DATETIME, UpdateBy VARCHAR(10)) 
create Table #tblMaster (MasterId INT IDENTITY(100,1), OldId int, DateTimeNow DATETIME, UpdateBy VARCHAR(10)) 
create Table #tblChild (ChildId INT IDENTITY(10,1), MasterId INT, UniqueKey VARCHAR(10), DateTimeNow DATETIME)  

insert into #tblData 
select * 
from (
    values 
    (1, 'uq1', getdate(), 'abc'), 
    (2, 'uq2', getdate()+1, 'xyz') 
) d (Id , UniqueKey , DateTimeNow, UpdateBy ) 

insert into #tblMaster 
select Id, DateTimeNow, UpdateBy 
from #tblData 

insert into #tblChild 
select MasterId, UniqueKey, d.DateTimeNow 
from #tblData d 
inner join #tblMaster m on m.OldId = d.Id 

select * from #tblChild 
select * from #tblMaster 

drop table #tblChild 
drop table #tblMaster 
drop table #tblData 
保存