如果我明白你的問題,你可以插入兩個階段,註釋保持舊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
來源
2013-11-26 22:42:09
ARA
DB什麼您使用的是?如果您使用SQL Server,則可以使用'OUTPUT'子句獲取標識列值。更好的是,如果有序列作爲主鍵,則可以在插入之前預先生成密鑰。 – Lock
MSSQL - 我不熟悉OUTPUT子句 –
@SeattleLeonard IDENTITY_SCOPE()返回上次創建的增量ID。這是你需要的嗎? – LINQ2Vodka