Sample Header Ad - 728x90

Does SQL Server read all of a COALESCE function even if the first argument is not NULL?

104 votes
8 answers
21540 views
I'm using a T-SQL COALESCE function where the first argument will not be null on about 95% of the times it is ran. If the first argument is NULL, the second argument is quite a lengthy process: SELECT COALESCE(c.FirstName ,(SELECT TOP 1 b.FirstName FROM TableA a JOIN TableB b ON .....) ) If, for example, c.FirstName = 'John', would SQL Server still run the sub-query? I know with the VB.NET IIF() function, if the second argument is True, the code still reads the third argument (even though it won't be used).
Asked by Curtis (1265 rep)
Sep 19, 2011, 03:06 PM
Last activity: Nov 4, 2019, 05:35 AM