2014-07-03 41 views
1

下面是我的表。克隆記錄與SQL服務器中的後代

CREATE TABLE [dbo].[CCMaster](
[CCId] [int] IDENTITY(1,1) NOT NULL, 
[GId] [int] NULL, 
[BKId] [int] NULL, 
[Class] [varchar](100) NULL, 
[ParentId] [int] NULL,) 

SET IDENTITY_INSERT CCMaster ON 

insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(1,33,162,'CORPORATE',NULL) 
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(10,33,162,'Call center related',4) 
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(11,33,162,'Channel related',2) 
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(12,33,162,'Advertisement',6) 
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(13,33,162,'Brand Ambassador',6) 
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(14,33,162,'OTAF/TVP (New Activation)',11) 
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(15,33,162,'Service Barred',7) 
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(16,33,162,'Call center behaviour',10) 
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(17,33,162,'Store Personnel behavior',8) 
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(2,33,162,'DTH',NULL) 
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(3,33,162,'2G',NULL) 
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(4,33,162,'3GS',NULL) 
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(5,33,162,'Broadband',NULL) 
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(6,33,162,'Brand',1) 
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(7,33,162,'Barring-related',3) 
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(8,33,162,'Behaviour related',4) 
insert into CCMaster([CCId],[GID],[BKID],[Class],[ParentId]) values(9,33,162,'Call center related',3) 

SET IDENTITY_INSERT CCMaster OFF 

該表中有成千上萬條記錄。我只想克隆GID = 33和BKID = 162的記錄。子項的父項必須與相應父項的自動生成的ID匹配。我嘗試了this查詢,但它不適合我。

我嘗試使用遊標,首先插入父母,然後試圖查詢孩子,但是這也沒有工作。任何幫助,將不勝感激。

+0

你想將數據複製到與ID列不同的值相同的表?或者你想將它複製到一個新表中?你能和我們分享一個CREATE TABLE語句的例子嗎? –

+0

我想將它複製到id列不同值的同一個表中。我會用表格結構更新我的答案 – Hitesh

+0

你能否澄清一下這句話「孩子的父母必須與各父母的自動生成的ID相匹配」?你的意思是,例如上面CCID = 6的克隆會得到6的parentID? –

回答

2

使用遞歸CTE得到你想要插入的行。

要找出新的價值就是被用作ParentId可以使用merge with output,感受一下老CCId和新CCId之間的映射。將合併輸出捕獲到表變量,合併後,可以從表變量更新ParentId

declare @T table 
(
    OldCCId int, 
    OldParentId int, 
    NewCCId int 
); 

with C as 
(
    select M.* 
    from dbo.CCMaster as M 
    where ParentId is null and 
     GId = 33 and 
     BKId = 162 
    union all 
    select M.* 
    from dbo.CCMaster as M 
    inner join C 
     on C.CCId = M.ParentId 
) 
merge into dbo.CCMaster as M 
using C 
on 1 = 0 
when not matched then 
    insert(GID, BKID, Class) 
    values (C.GID, C.BKID, C.Class) 
output C.CCId, 
     C.ParentId, 
     inserted.CCId 
    into @T; 

update M 
set ParentId = T2.NewCCId 
from CCMaster as M 
    inner join @T as T1 
    on M.CCId = T1.NewCCId 
    inner join @T as T2 
    on T1.OldParentId = T2.OldCCId; 

SQL Fiddle

+0

感謝Mikael,完美地工作。我從來不知道使用合併,這促使我開始使用它。 – Hitesh

+0

@HiteshSalian很棒。有一些您需要注意的合併報告問題。他們可能會適用於你,或者他們可能不會,但是瞭解他們並不會傷害他們。 [請謹慎使用SQL Server的MERGE語句](http://www.mssqltips.com/sqlservertip/3074/use-caution-with-sql-servers-merge-statement/) –

+0

哦,非常感謝Mikael。如果我受到它的影響,請牢記這一點。 – Hitesh