2013-11-15 42 views
2

以下是此場景:SQL遷移腳本 - 嵌套循環

我正在將數據從舊系統遷移到新系統。

舊系統有2個表格代表註釋及其回覆。

新系統有一個允許嵌套評論的評論表。所以它有一個自引用的外鍵。

我需要從2個表中的數據移動到1

這裏的問題: 雖然我知道哪些子評論都涉及到其父母的意見,而我做的插入到新表,我不知道父母評論的新ID。

我已經使用一個while循環遍歷每個父評論然後執行循環內的2個插入考慮。

這是一個適當的時間來使用遊標?根據幾乎每個人的建議,我避免他們像瘟疫。

你能想出不同的方法來從2個表將數據移動到1?

所有這些都發生在另一個while循環內部。我也想知道是否應該嘗試將此循環分解爲單獨的循環而不是嵌套它們。

+3

DB什麼您使用的是?如果您使用SQL Server,則可以使用'OUTPUT'子句獲取標識列值。更好的是,如果有序列作爲主鍵,則可以在插入之前預先生成密鑰。 – Lock

+0

MSSQL - 我不熟悉OUTPUT子句 –

+2

@SeattleLeonard IDENTITY_SCOPE()返回上次創建的增量ID。這是你需要的嗎? – LINQ2Vodka

回答

0

所以它看起來像如果我使用SQL 2008或以上,我可以使用MERGE聲明與OUTPUT關鍵字。不幸的是,我需要支持沒有MERGE語句的SQL 2005。我結束了使用嵌套循環。

1

沒有在我面前的測試數據庫,可以使用在MSSQL中OUTPUT關鍵字做到這一點。應該足以讓你開始:

DECLARE @NewIDs Table 
(
    NewID INT, 
    OldID INT 
) 

INSERT INTO NewTable 
OUTPUT NewTable.ID, 
     OldTable.ID 
INTO @NewIDs 
SELECT NULL As ParentCommentID, Othercolumns 
FROM OldParentTable 

INSERT INTO NewTable 
SELECT NewID As ParentCommentID, OtherColumns 
FROM OldChildTable 
JOIN @NewIDs NewIDs 
    ON NewIDs.OldID = OldChildTable.OldParentTableID 
+0

嗯,當我採取這種方法時,我得到錯誤「多部分標識符」OldTable.ID「無法綁定。「 –

+0

fromColumn狀態的文檔:是一個列前綴,它指定包含在用於指定要更新或刪除的行的DELETE,UPDATE或MERGE語句的FROM子句中的表。 –

1

如果我明白你的問題,你可以插入兩個階段,註釋保持舊ID在你的餐桌,參照舊的意見,使孩子(舊回覆)的第二個插入firstr插入。

你也可以使用IDS單獨的表,如果你不想改變你的新表

if object_id('oldReply') is not null 
    drop table oldReply 
if object_id('oldComment') is not null 
    drop table oldComment 
if object_id('newComment') is not null 
    drop table newComment 
go 
create table oldComment (
    id integer identity(1,1) primary key, 
    msg varchar(64) 
    ) 
create table oldReply(
    id integer identity(1,1) primary key, 
    msg varchar(64), 
    commentId integer references oldComment(id) 
    ) 
create table newComment (
    id integer identity(1,1) primary key, 
    msg varchar(64), 
    parentId integer references newComment(id), 
    oldCommentId integer 
) 
go 
insert into oldComment(msg) values ('m1'), ('m2'), ('m3') 
insert into oldReply(msg, commentId) values ('r1', 1) , ('r2', 2), ('r3', 3) 

select * from oldComment 
select * from oldReply 

insert into 
newComment(msg, oldCommentId) 
    select msg, id from oldComment 
    ; 
insert into newComment (msg, parentId) 
    select oldREply.msg, parent.id 
    from oldReply 
    inner join newComment parent on oldReply.commentId = parent.oldCommentId 
    ; 
--to check 
select * from newComment