如果我明白你的问题,你可以插入两个阶段,注释保持旧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