使用更新的父子键导入数据
我有一个具有以下结构的表:
[ID] [int] IDENTITY(1,1) NOT NULL,
[ParentID] [int] FOREIGN KEY([ParentID]) REFERENCES [dbo].[tblTask] ([ID])
[Name] [varchar](20)
该表已填充。
我需要从另一个类似的表导入数据,并且当然需要新的父子关系有效。是否有一些我可以使用的预先存在的功能或方法?
这是 MSSQL 2008
I have a table with this structure:
[ID] [int] IDENTITY(1,1) NOT NULL,
[ParentID] [int] FOREIGN KEY([ParentID]) REFERENCES [dbo].[tblTask] ([ID])
[Name] [varchar](20)
This table is populated.
I need to import data from another similar table, and need the new parent-child relationships to be valid of course. Is there some pre-existing functionality or methodology that I can use?
This is MSSQL 2008
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从评论来看,您似乎是在不必考虑其他并发插入的环境中进行此导入。
因此,在这种情况下,您只需
SET IDENTITY_INSERT ON
然后对源 id 应用偏移量,这样它们最终会比目标表中任何预先存在的 id 更大。From the comments it seems you are doing this import in an environment where you don't have to consider other concurrent inserts.
So in this case you can just
SET IDENTITY_INSERT ON
then apply an offset to the source ids so they will end up larger than any pre-existing ids in the destination table.