Why does the MS Sql server (2005 and 2008, not verified elsewhere) cause an error if the column does not exist in the table before it causes an error if the table does not exist?
In particular, I have the following scheme (very abbreviated to show everything important):
CREATE TABLE table1 ( id int identity(1,1) primary key )
Why the following query failed with an Invalid column name 'iDoNotExist'.
?
if 1=2 begin print 'table that shouldn''t exist does' select * from [IDoNotExist] end if 1=2 begin print 'column that shouldn''t exist does' select iDoNotExist from table1 end
I expect that it will either work with a few errors (as if it really compiled and noticed that the table and column are not there) or there are no errors (as if it ignored the contents of the if statements, since they were not going to work). What can I do to run it without errors?
PS the actual request has this in if statements, but it does not make any difference:
exists (select * from [INFORMATION_SCHEMA].[COLUMNS] t where t.[TABLE_NAME] = 'table1' and t.[COLUMN_NAME] = 'iDoNotExist')
source share