last day (15 days later) » 

9:15 AM
2
Q: If Exists behaves differently depending on column or table missing

JohnIm trying to understand why this code, in my opinion, behaves inconsistently. I have a simple if exists clause which tests if a column is present in a table or not. IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'COL' and TABLE_NAME = 'TAB') BEGIN PRINT 'EXISTS' ...

 
What version of SQL Server are you using?
 
Tested with 2014 and 2017. Both same behaviour.
 
This is because the query inside the IF statement is only parsed for syntax accuracy. The error is a runtime error and throws the exception during execution. You can verify this by parsing your query in SSMS (the blue check mark button).
 
@SeanLange but why doesn't it throw a "table does not exist" error when the table doesn't exist? I would expect it to.
 
Perhaps you can elaborate further? In both cases if exists is evaluated to false so only the else case is executed. In this case i don´t understand why SELECT NOTEXISTINGCOL FROM NOTEXISTINGTAB is valid but SELECT NOTEXISTINGCOL FROM EXISTINGTAB raises an error.
 
9:15 AM
Indeed -- I have tried to replicate what you have done and I never get any errors.
 
@SeanLange yeah but in the second query if you exchange FANTASYTABLE with a real table you cant execute it anymore.
 
@avery_larry I get the error when the SELECT statement under "Exists" contains a table that exists, but a column that does not exist in that table.
 
I think the second query in the OP is confusing the issue. It is really two different versions of the first query. One with a valid table but an invalid column name causes a compile error. With an invalid table name it becomes a run time error. Quite strange indeed. It is sort of behaving a little bit like deferred name resolution. Maybe there is a bit of a quirk from that here?
 
@avery_larry write an exists query which is always evaluated to false like (Select * from XXX where 1 = 0).
 
I see the issue now. Strange.
 
9:15 AM
Yes, apparently the pre-parser only throws an error if the table exists. If it doesn't exist, it leaves it for the run-time parser. It may be useful to the OP to know that this has nothing to do with what's in the IF EXISTS. It has to do with how the pre-parser performs semantic checks. If this behavior is documented anywhere, however, I have not been able to find it.
 
I don't know about that. For instance, you can change the select statement to: select 5/0 and it doesn't throw an error unless the if exists returns true.
And it doesn't throw an error with an invalid table. Only an error with an invalid column in an existing table.
 
I don't think division by zero is an error the pre-parser would look for. I think it's only a run-time error.
 
For the second one, the IF is always evaluated to false (WHERE 1=0). The optimizer is smart enough to remove the code branch.
For the first one, it can be true or false in the runtime.
 
@PeterHe the optimizer is not that smart. Inside an exists where 1 = 0 if you reference an invalid column for an existing table it will throw an error.
 
IF you check the Execution plan, you only see the IF EXISTS. Everyting, including the SELECT THISISMYFANTASYCOLUMN FROM FANTASYTABLE, in the if branch is eliminated.
I think it is also related to statement level compiling
When the IF is complied and executed, and evaluated to FALSE, the branch is not complied at all.
 
Some things seem to be compiled and bound at batch parsing time, and some things seem to be compiled and bound at run time. I've always found that tables seem to be run-time bound, but columns are compile-time bound. It's very odd and, imho, inconsistent. When creating SQL to run across versions where a table may or may not exist, it's easy. When a column may or may not exist, I always have to resort to dynamic SQL.
 
@avery_larry its kind of similar question. To avoid the problem altogether better use dynamic sql. But the question specifically is why sql-server is treating columns more strictly than tables. Sean Lange boiled the issue down to just two lines to illustrate the different behavior. After years working with sql-server i find this behaviour quite odd and unintuitive hence the question. If there is a technical explanation for this behaviour i would love to read about it if such documentation exists.
 

last day (15 days later) »