Can someone explain why the first statement works, but the second one generates an error if myfunc() does not exist?
1) if exists(select name from sysobjects where name = 'myfunc') select dbo.myfunc();else select 0;
2) select case when exists(select name from sysobjects where name = 'myfunc') then dbo.myfunc() else 0 end;
Generates this error if myfunc() does not exist, even though it should not be executing that case:
Cannot find either column "dbo" or the user-defined function or aggregate "dbo.myfunc", or the name is ambiguous.