Using merge..output to get mapping between source.id and target.id(使用 merge..output 获取 source.id 和 target.id 之间的映射)
问题描述
非常简单,我有两个表 Source 和 Target.
Very simplified, I have two tables Source and Target.
我想将所有行从 @Source
移动到 @Target
并知道每个 SourceID
的 TargetID
> 因为还有表 SourceChild
和 TargetChild
也需要复制,我需要将新的 TargetID
添加到 TargetChild.TargetID
FK 列.
I would like to move all rows from @Source
to @Target
and know the TargetID
for each SourceID
because there are also the tables SourceChild
and TargetChild
that needs to be copied as well and I need to add the new TargetID
into TargetChild.TargetID
FK column.
对此有几种解决方案.
- 使用 while 循环或游标一次向 Target 插入一行 (RBAR),并使用
scope_identity()
填充TargetChild
的 FK. - 将临时列添加到
@Target
并插入SourceID
.然后,您可以加入该列以获取TargetChild
中 FK 的TargetID
. SET IDENTITY_INSERT OFF
用于@Target
并自己处理分配新值.您将获得一个范围,然后在TargetChild.TargetID
中使用该范围.
- Use a while loop or cursors to insert one row (RBAR) to Target at a time and use
scope_identity()
to fill the FK ofTargetChild
. - Add a temp column to
@Target
and insertSourceID
. You can then join that column to fetch theTargetID
for the FK inTargetChild
. SET IDENTITY_INSERT OFF
for@Target
and handle assigning new values yourself. You get a range that you then use inTargetChild.TargetID
.
我不是很喜欢他们中的任何一个.到目前为止,我使用的是游标.
I'm not all that fond of any of them. The one I used so far is cursors.
我真正想做的是使用插入语句的 output
子句.
What I would really like to do is to use the output
clause of the insert statement.
但这是不可能的
但合并是可能的.
结果
我想知道你有没有用过这个?如果您对解决方案有任何想法或发现任何问题?它在简单的场景中工作正常,但当查询计划由于复杂的源查询而变得非常复杂时,可能会发生一些丑陋的事情.最坏的情况是 TargetID/SourceID 对实际上不匹配.
I want to know if you have used this? If you have any thoughts about the solution or see any problems with it? It works fine in simple scenarios but perhaps something ugly could happen when the query plan get really complicated due to a complicated source query. Worst scenario would be that the TargetID/SourceID pairs actually isn't a match.
MSDN 对 输出from_table_name 有此说明> 子句.
MSDN has this to say about the from_table_name
of the output clause.
是列前缀,用于指定包含在 DELETE、UPDATE 或 MERGE 语句的 FROM 子句中的表,用于指定要更新或删除的行.
Is a column prefix that specifies a table included in the FROM clause of a DELETE, UPDATE, or MERGE statement that is used to specify the rows to update or delete.
出于某种原因,他们不说要插入、更新或删除的行",而只说要更新或删除的行".
For some reason they don't say "rows to insert, update or delete" only "rows to update or delete".
欢迎提出任何想法,非常感谢对原始问题的完全不同的解决方案.
Any thoughts are welcome and totally different solutions to the original problem is much appreciated.
推荐答案
在我看来,这是 MERGE 和输出的一个很好的用途.我已经在几个场景中使用过,到目前为止还没有遇到任何奇怪的情况.例如,这里是将文件夹及其中的所有文件(身份)克隆到新创建的文件夹 (guid) 中的测试设置.
In my opinion this is a great use of MERGE and output. I've used in several scenarios and haven't experienced any oddities to date. For example, here is test setup that clones a Folder and all Files (identity) within it into a newly created Folder (guid).
这篇关于使用 merge..output 获取 source.id 和 target.id 之间的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!