关于复杂选择查询的SQL UPDATE语句

时间:2017-09-05 08:13:39

标签: sql sql-server sql-update

我有一个包含许多连接的复杂选择查询。它是从工具生成的。我必须根据该查询更新字段。

我尝试解码但不确定基于我的解码更新是否正确。我可以根据工具生成的查询更新值。如下所示: -

UPDATE F_Sales SET d_source = "XYZ" WHERE
F_Sales.customer_code in (SELECT A, B, C, D......... FROM K, L, M, N, O,P ....)

2 个答案:

答案 0 :(得分:1)

create table #temp(customer_code INT)
insert into #temp SELECT A, B, C, D......... FROM K, L, M, N, O,P ....

UPDATE F_Sales SET d_source = "XYZ" 
FROM F_Sales  join #temp ON
F_Sales.customer_code = #temp.customer_code

答案 1 :(得分:0)

如果其中一个A,B, ..列(例如,列D)可以映射到F_Sales.customer_code

UPDATE F_Sales SET d_source = "XYZ" 
WHERE
F_Sales.customer_code in (
  SELECT D 
  FROM ( -- untouched original query
     SELECT A, B, C, D......... FROM K, L, M, N, O,P ....) q
)

UPDATE F_Sales SET d_source = "XYZ" 
FROM F_Sales 
JOIN ( -- untouched original query
     SELECT A, B, C, D......... FROM K, L, M, N, O,P ....) q
ON F_Sales.customer_code = q.D

如果您可以显示生成的查询

,我们可能会做得更好