有没有一种方法可以从一个到另一个使用 FK 批量插入到两个表中?

发布于 2024-09-10 08:47:52 字数 753 浏览 3 评论 0原文

我将给出我当前方法的伪代码示例,如果有人知道一种不能一次只工作一行的方法,我将非常感激。我正在使用 MS SQL Server 2008。

define cursor for the data to be inserted (about 3 million records)
loop
(
    insert record into table 1
    use scope_identity() to get key
    insert record into table 2 that references table 1
)

我更愿意同时对两个表进行某种插入,因为游标和循环很慢。

在有人抱怨为什么我要向两个具有 1 对 1 关系的单独表中插入某些内容之前,这是因为第一个表无法修改,并且我需要第二个(临时)表中的信息作为稍后数据转换操作的参考。

不,我无法添加临时列来保存无法修改的表的引用数据,因为它无法修改。这是一个实时系统,我无权更改表。


附加信息:

Source:

1 a big  monkey
2 a tall elephant
3 a big  giraffe
4 a tiny cow
5 a tall cow

Dest:

Table 1       Table 2
monkey        1 big
elephant      2 tall
giraffe       3 big
cow           4 tiny
cow           5 tall

I'll give a pseudocode example of my current method and if anyone knows of a method that doesn't work one row at a time, I'd be quite appreciative. I'm using MS SQL Server 2008.

define cursor for the data to be inserted (about 3 million records)
loop
(
    insert record into table 1
    use scope_identity() to get key
    insert record into table 2 that references table 1
)

I'd much rather do some sort of insert into both tables simultaneously because a cursor and loop are slow.

Before anyone rants about why I'm inserting something into two separate tables that has a 1 to 1 relationship, it's because the first table cannot be modified and I need the information in the second (temporary) table for reference for data conversion operations later on.

No, I cannot add a temporary column to hold the reference data on to the table that cannot be modified because it cannot be modified. This is a live system and I don't have permissions to alter the table.


Additional Info:

Ex

Source:

1 a big  monkey
2 a tall elephant
3 a big  giraffe
4 a tiny cow
5 a tall cow

Dest:

Table 1       Table 2
monkey        1 big
elephant      2 tall
giraffe       3 big
cow           4 tiny
cow           5 tall

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

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

发布评论

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

评论(4

桃扇骨 2024-09-17 08:47:52

您可以在上使用合并 >Table1输出 进入Table2

merge Table1
using SourceTable
on 0 = 1
when not matched then
  insert (Animal) values (SourceTable.Animal)
output inserted.ID, SourceTable.Size into Table2(ID, Size);

SQL Fiddle

注意: 如果 Table2< /code> 有一个针对 Table1 定义的外键,您无法直接输出到 Table2。在这种情况下,您可以使用临时表作为输出目标,并从临时表插入到 Table2 中。

You can use merge on Table1 and and output into Table2.

merge Table1
using SourceTable
on 0 = 1
when not matched then
  insert (Animal) values (SourceTable.Animal)
output inserted.ID, SourceTable.Size into Table2(ID, Size);

SQL Fiddle

Note: If Table2 has a foreign key defined against Table1 you can't do the output directly to Table2. In that case you can use a temporary table as the target of the output and insert into Table2 from the temporary table.

别把无礼当个性 2024-09-17 08:47:52

对于如此多的数据,最好的选择可能是隔离系统上的更新,允许身份插入并在插入之前预先填充密钥。


或者,您可以只执行第一次插入,然后修改第二次插入(来自临时表)以连接到原始数据并找到键。

With that much data, the best option might be to isolate updates on the system, allow identity insert and prepoulate the keys before inserting.


Or, can you just do the first insert, then modify the insert for the second (from a temp table) to join on to the original data and find the key.

时光是把杀猪刀 2024-09-17 08:47:52

使用 IDENTITY_INSERT、VIEW 和 INSTEAD OF 触发器,并通过插入预填充标识值。

查看此 SQL Fiddle

我尝试在 INSTEAD OF 触发器中尝试各种操作,让 T1 的标识列用于 T2,但最终失败了。

Use IDENTITY_INSERT, a VIEW and an INSTEAD OF trigger, and prepopulated identity values with your insert.

See this SQL Fiddle.

I tried flailing about with various things in the INSTEAD OF trigger to let the T1's identity column be used for T2, but ultimately I failed.

焚却相思 2024-09-17 08:47:52
insert into table1
select substring(src.data, 8 /* assuming fixed length as exampled */, len(src.data))
from source src

insert into table2
select t1.id, substring(src.data, 3 /* skip 'a ' */, 7)
from
    table1 t1
        inner join source src
        on substring(src.data, 8, len(src.data)) = t1.data

对于给定的例子,我不能做得更好......

insert into table1
select substring(src.data, 8 /* assuming fixed length as exampled */, len(src.data))
from source src

insert into table2
select t1.id, substring(src.data, 3 /* skip 'a ' */, 7)
from
    table1 t1
        inner join source src
        on substring(src.data, 8, len(src.data)) = t1.data

For the given example, i cannot do better...

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