Sql server 如何在从表本身更新同一个表时为表添加别名?

Sql server 如何在从表本身更新同一个表时为表添加别名?,sql-server,sql-update,Sql Server,Sql Update,当从表本身更新同一个表时,如何为表添加别名 我的问题是: update accounts set adusername = x.adusername from Accounts x where x.AccountName = accounts.rev and x.ok>10 and accounts.ok in (0,1,2,3) and x.ADUserName is not null and accounts.ADUserName is null 错误 Msg 4104, Lev

当从表本身更新同一个表时,如何为表添加别名

我的问题是:

update accounts set adusername = x.adusername 
from Accounts x
where x.AccountName = accounts.rev and x.ok>10 and accounts.ok in (0,1,2,3) 
and x.ADUserName is not null and accounts.ADUserName is null
错误

Msg 4104, Level 16, State 1, Line 9
The multi-part identifier "accounts.rev" could not be bound.
Msg 4104, Level 16, State 1, Line 9
The multi-part identifier "accounts.ok" could not be bound.
Msg 4104, Level 16, State 1, Line 9
The multi-part identifier "accounts.ok" could not be bound.
Msg 4104, Level 16, State 1, Line 9
The multi-part identifier "accounts.ok" could not be bound.
Msg 4104, Level 16, State 1, Line 9
The multi-part identifier "accounts.ok" could not be bound.
Msg 4104, Level 16, State 1, Line 9
The multi-part identifier "accounts.ADUserName" could not be bound.
这个怎么样:

UPDATE a
SET a.adusername = x.adusername 
FROM Accounts x
INNER JOIN Accounts a ON x.AccountName = a.rev
WHERE x.ok>10
    AND a.ok in (0,1,2,3) 
    AND x.ADUserName is not null
    AND a.ADUserName is null

这仅适用于子查询或联接:

UPDATE accounts SET adusername = 'ABC'
FROM Accounts AS x
WHERE x.ID IN (SELECT ID FROM Accounts WHERE Accounts.Username = 'XYZ') AND x.OK in (0,1,2,3)


应该做这项工作。

请提供一些示例数据。@Ullas这是我正在处理的实际查询
UPDATE accounts SET adusername = x.adusername 
FROM Accounts
  JOIN Accounts AS x ON Accounts.ID = x.ID
WHERE x.Username = 'XYZ' AND Accounts.OK in (0,1,2,3)