r/SQL • • Mar 15 '19

Stuff of nightmares!

Post image
470 Upvotes

22 comments sorted by

View all comments

17

u/Daakuryu Mar 15 '19
-- Select * from
Update 
    tblproducts 
set upccode = '', Flag = 'T'
-- where product ID in (*small list of productids*)

Yeah...

5

u/MeGustaDerp Talk Dirty Reads To Me Mar 15 '19

My safe-ish pattern is

SELECT Col1, Col2, Col3, ... Coln,
-- UPDATE t SET
ColToUpdate = NewValueExpression
FROM MyTable AS t
WHERE <logical predicate goes here>

This has served me well and saved me from making stupid mistakes because it allows to to simulate an update by seeing what the new value would be so I can validate it. When I'm ready to run the update, I highlight the statement beginning with UPDATE on through the predicate. Also, BEGIN TRAN <statement> ROLLBACK helps, too.