UPDATE table1
SET col = 1
WHERE col2 = 'xyz'
UPDATE a
SET col = 1
FROM table1 a
WHERE col2 = 'xyz'
Which is preferred ?
One difference I could find out was that the second statement allows to use WITH NO LOCK near the 'FROM' statement. But when used near the update statement, it gives the same error message as that of the first statement
The NOLOCK and READUNCOMMITTED lock hints are not allowed for target tables of INSERT, UPDATE or DELETE statements.
Thanks So Much
They are the same.
If the UPDATE statement only involves accessing a single table, the 1st is cleaner/simpler.
If you need to do e.g. a JOIN, then assigning an alias per your 2nd example is (IMO) cleaner, like:
UPDATE a
SET a.Col = 1
FROM TableA a
JOIN TableB b ON a.Id = b.Id
WHERE b.Something = '123'
BTW, there is no point specifying a NOLOCK hint on the table being updated, so don't count that as a reason for one over the other.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With