• Final note: I do want to stress that the message that the author tried to convey is correct and relevant. He just chose an incorrect context.

    But if you need to test for existence of a row and are in doubt between either (a) SELECT ... FROM ... / IF @@ROWCOUNT = 0; or (b) IF (SELECT COUNT(*) FROM ...) = 0; or (c) IF EXISTS (SELECT * FROM ...) - than always go for option c. There will be no measurable difference if the row does not exist, but if the row does exist, the third option will save time by stopping after the first match, whereas options a and b will continue to look for and process / count the remaining matches.

    And when possible, try to make sure that there is an index available for the optimizer to use an efficient index seek instead of having to scan all rows....


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/