问题描述
我需要一个 SQL 语句,用 #T2(C1) 中的值填充 #T1 表第二列中的空值.
I need a SQL statement which fills the null values from the second column of #T1 table with values from #T2(C1).
这两个表的列之间没有外键或匹配项.
There is no foreign key or match between the columns of those two tables.
示例:
更新后,T1 将如下所示:
After update, the T1 will look like:
我找到了两种方法:
使用 CTE
带有派生表
我的问题是,我可以在不使用窗口函数且不使用光标的情况下实现这一点吗?
My question is, can I achieve this without using windowing functions and with no cursors?
更新
@Damien_The_Unbeliever 正确地指出要进行这种更新,如果不定义表的排序是不可能的,实际上我认为确切地说是没有正确识别和链接目标表中的行.
@Bogdan Sahlean 找到了另一种方法,使用表变量和 IDENTITY 列,我对这个解决方案很满意,这是另一种方法但是,在实际应用中我仍然会使用窗口函数
谢谢大家
Update
@Damien_The_Unbeliever correctly points that to do this kind of update it is not possible without defining an ordering on tables, actually I think exactly said is without properly identify and link the rows from target table.
@Bogdan Sahlean has found another way, using table variables and IDENTITY column, which I'm happy with this solution, it's another way
However, in the real application I will still use the windowing functions
Thanks all
推荐答案
1.我想你在目标表 (#T1) 中有一个 pk
.
1.I suppose you have a pk
in target table (#T1).
2.该解决方案使用 IDENTITY(1,1)
列和两个表变量代替 ROW_NUMBER.
2.Instead of ROW_NUMBER this solution uses IDENTITY(1,1)
columns and two table variables.
3.我没有测试过这个解决方案.
3.I didn't tested this solution.
这篇关于当两个表之间没有关系时在它们之间更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!