将两个表之间的一对多关系复制到另外两个表(SQL 2005)

发布于 2024-09-28 07:50:08 字数 305 浏览 0 评论 0原文

我有两个具有父子关系的表。我想将他们的一些记录复制到另外两个表中,也具有父子关系,但表结构略有不同。

两组表都涉及一个外键,即整数列。所有表的主键列都具有标识增量。

如果我从源父表到目标父表执行SELECT INTO,则目标记录的主键值将与源记录不同,并且父子关系将丢失。

有谁知道如何在复制过程中保留这种关系,因为我将在新的父表中拥有新的主键值?我不想在此复制过程中为新表设置标识增量关闭,因为无法保证源表中的主键值不会已位于目标中。

希望我的描述有意义,并感谢您的意见。如果我可以进一步澄清,请告诉我。

I have two tables with a parent-child relationship. I would like to copy some of their records to two other tables, also with a parent-child relationship, but with a slightly different table structure.

There is a foreign key involved with both sets of tables, an integer column. All tables have the identity increment on for their primary key columns.

If I do a SELECT INTO from the source parent table to the destination parent table, the primary key values for the destination records will be different from the source records, and the parent-child relationship will be lost.

Does anyone know how I can preserve this relationship during the copy, given that I'll have new primary key values in the new parent table? I'd prefer not to set the identity increment off for the new tables during this copy procedure, because there's no guarantee the primary key values in the source table won't already be in the destination.

Hope my description makes sense, and thanks for your opinions. Let me know if I can clarify further.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪异←思 2024-10-05 07:50:08

完成此操作后,我通过在插入新表后保留旧 ID 来实现此目的。

有些人使用旧 ID 添加临时列,但我知道更好的方法。

您可以使用 OUTPUT 子句 将插入的记录插入到临时表中(用新旧ID作为映射表。然后在插入子记录时加入该表。

像这样(粗略)

DECLARE @MyTableVar TABLE (
    NewID INT,
    OldID INT
)

INSERT NewParentTable
OUTPUT
    INSERTED.ID,
    old.ID
INTO @MyTableVar
SELECT *
FROM OldParentTable old


INSERT NewChildTable
SELECT
    old.ID,
    t.NewID,
    old.name
FROM OldChildTable old
INNER JOIN @MyTableVar t ON t.OldID = old.ParentID

When I have done this, I did so by preserving the old ID after the insert into the new table.

Some people add a temporary column with the old ID, but I know of a better way.

You can use the OUTPUT clause to insert the inserted records into a temp table (with the new and the old IDs as a mapping table. Then join to that when inserting the child records.

Something like this (crude)

DECLARE @MyTableVar TABLE (
    NewID INT,
    OldID INT
)

INSERT NewParentTable
OUTPUT
    INSERTED.ID,
    old.ID
INTO @MyTableVar
SELECT *
FROM OldParentTable old


INSERT NewChildTable
SELECT
    old.ID,
    t.NewID,
    old.name
FROM OldChildTable old
INNER JOIN @MyTableVar t ON t.OldID = old.ParentID
壹場煙雨 2024-10-05 07:50:08

您必须将原始 ID 与相应的 ParentID 一起维护在表中的单独列中。
因此,在目标表中,您需要添加一个parentID_orig 列(不是自动编号)以保留链接。

You'd have to maintain the original ID in a separate column in the table with the corresponding parentID.
So in your destination table, you'd need to add an parentID_orig column (not auto-number) to retain the link.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文