我有两个临时表。第一个是:
Column1 |Column2 |Column3 |Column4 |Column5 |Column6
--------|----------------|--------------|------------|------------|-------
10 |11 |1111 |001 |345.000000 |22
13 |12 |1111 |001 |700.000000 |22
10 |14 |1112 |001 |345.000000 |22
16 |15 |5454 |001 |1200.000000 |22
16 |17 |2364 |001 |1350.000000 |22
第二个数字将替换前两列中的数字:
Column1 |Column2 |
--------|----------------|
10 |1 |
11 |2 |
12 |5 |
13 |3 |
14 |0 |
15 |7 |
16 |2 |
17 |5 |
我必须更改第一个表(表的前两列),以便用相应的新值替换数字
结果将如下:
Column1 |Column2 |Column3 |Column4 |Column5 |Column6
--------|----------------|--------------|------------|------------|-------
1 |2 |1111 |001 |345.000000 |22
3 |5 |1111 |001 |700.000000 |22
1 |0 |1112 |001 |345.000000 |22
2 |7 |5454 |001 |1200.000000 |22
2 |5 |2364 |001 |1350.000000 |22
我完全被困住了
答案 0 :(得分:0)
我将对连接使用可更新的CTE:
WITH cte AS (
SELECT t1.Column1 AS col1_target, t2_a.Column2 AS col1_src,
t1.Column2 AS col2_target, t2_b.Column1 AS col2_src
FROM #temp1 t1
INNER JOIN #temp2 t2_a ON t1.Column1 = t2_a.Column1
INNER JOIN #temp2 t2_b ON t1.Column2 = t2_b.Column2
)
UPDATE cte
SET
col1_target = col1_src,
col2_target = col2_src;
答案 1 :(得分:0)
您可以使用两个left join
:
update t1
set t1.column1 = t2_1.column2,
t1.column2 = t2_2.column2
from table1 t1 left join
table2 t2_1
on t1.column1 = t2_1.column1 left join
table2 t2_2
on t1.column2 = t2_2.column1
where t2_1.column1 is not null or t2_1.column2 is not null