如果我理解您的问题,您可以分两个阶段进行插入,首先插入评论,将旧 ID 保留在您的表中,以便参考旧评论进行第二次插入子项(旧回复)。
如果您不想更改新表,也可以为 id 使用单独的表
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