有没有一种方法可以从一个到另一个使用 FK 批量插入到两个表中?
我将给出我当前方法的伪代码示例,如果有人知道一种不能一次只工作一行的方法,我将非常感激。我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在
上使用合并 >Table1
和输出 进入Table2
。SQL Fiddle
注意: 如果
Table2< /code> 有一个针对
Table1
定义的外键,您无法直接输出到Table2
。在这种情况下,您可以使用临时表作为输出目标,并从临时表插入到Table2
中。You can use merge on
Table1
and and output intoTable2
.SQL Fiddle
Note: If
Table2
has a foreign key defined againstTable1
you can't do the output directly toTable2
. In that case you can use a temporary table as the target of the output and insert intoTable2
from the temporary table.对于如此多的数据,最好的选择可能是隔离系统上的更新,允许身份插入并在插入之前预先填充密钥。
或者,您可以只执行第一次插入,然后修改第二次插入(来自临时表)以连接到原始数据并找到键。
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.
使用 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.
对于给定的例子,我不能做得更好......
For the given example, i cannot do better...