Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need some clarification on SELECT within UPDATE t-SQL statement

Say, if I have the following t-SQL statement (designed to run on SQL Server 2008):

UPDATE tbl
SET col1 = (
    SELECT MAX(col1) FROM tbl AS t1 WHERE t1.type = tbl.type
);

How exactly does SELECT work in this case:

  1. It selects taking into account the results of each UPDATE, or

  2. It selects from the tbl as it was before the UPDATE began updating records.

Can someone clarify this for me?

like image 317
ahmd0 Avatar asked Nov 04 '22 19:11

ahmd0


1 Answers

2! Your subquery SELECT pulls the value as determined before the UPDATE makes any changes.

like image 102
ErikE Avatar answered Nov 09 '22 13:11

ErikE