更新并自行加入

时间:2016-04-21 21:31:19

标签: sql sql-server tsql

我想使用具有相同客户端编号的非空白列更新客户端表中的空白名称列。你知道我应该如何修改下面的SQL吗?谢谢,

UPDATE CLIENT
    SET NAME=(SELECT NAME FROM CLIENT WHERE (NAME IS NOT NULL AND NAME<>'') )
FROM CLIENT C1, CLIENT C2
WHERE (NAME IS NULL OR NAME='') AND C1.CLIENT_NO=C2.CLIENT_NO

1 个答案:

答案 0 :(得分:1)

I would recommend using window functions for this purpose:

with toupdate as (
      select c.*, max(name) over (partition by client_no) as maxname
      from client c
     )
update toupdate
    set name = maxname
    where name is null or name = '';