2016-04-28 3 views
3

Wie alias Sie eine Tabelle während Sie die gleiche Tabelle von sich selbst aktualisieren?Wie alias eine Tabelle während der Aktualisierung der gleichen Tabelle von sich selbst?

Meine Suche:

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 

Fehler

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. 
+0

Bitte einige Beispieldaten angeben. – Wanderer

+0

@Ullas, das ist die eigentliche Abfrage, mit der ich arbeite –

Antwort

3

Wie wäre es damit:

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 
2

Dies ist nur möglich mit Unterabfragen oder Joins:

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) 

oder

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) 

sollte die Arbeit machen.

Verwandte Themen