Re: IF Statement causes Invalid Column Name
From: Tibor Karaszi (tibor_please.no.email_karaszi_at_hotmail.nomail.com)
Date: 03/29/04
- Next message: Johannes Lebek: "JDBC batch error message without executeBatch() call"
- Previous message: Richard J: "RE: Latest entry..."
- In reply to: Donmn: "IF Statement causes Invalid Column Name"
- Next in thread: Donmn: "Re: IF Statement causes Invalid Column Name"
- Reply: Donmn: "Re: IF Statement causes Invalid Column Name"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 29 Mar 2004 15:46:50 +0200
This has to do with parse and compile time versus execute time. And also deferred (delayed) name resolution.
If you refer to a column which doesn't exist, SQL Server will not be able to parse your SQL, so you will never
get to the execute stage.
So, why does it work for a table? Answer is deferred name resolution. If the table doesn't exists, SQL Server
tries to resolve the table name at execution time. In other words, we have deferred name resolution for table
names, but not for column names.
If you want to ensure that tables, columns etc exists, do a check against the INFORMATION_SCHEMA views, the
system table or use the system functions (like OBJECT_ID()).
-- Tibor Karaszi, SQL Server MVP http://www.karaszi.com/sqlserver/default.asp "Donmn" <anonymous@discussions.microsoft.com> wrote in message news:C7DE5EEB-1918-4FB7-B9FA-32E35D91289F@microsoft.com... > Hi > > Thanks in advance for any help and please note I am relatively new to SQL. > > I am supporting an update script running in the Analyzer. I am having issues with an IF statement: > > When an IF statement references a missing column within, it produces an "Invalid Column Name". But, surely this should be validated when the IF statement equates to TRUE. > > On the other hand when the IF statement refers to a table which doesn't exist it has no problem. I have isolated the code for review. Any help would be appreciated and an answer would be excellent. > > Kind Regards > Donmn > > /*tblTMP.lngMISSINGCOL does not exist and tblTMP.tmpID = 1 Returns: Invalid Column Name*/ > IF ((SELECT [tmpID] FROM [dbo].[tblTMP]) = 0) > BEGIN > PRINT 'IF True' > SELECT tblTMP.lngMISSINGCOL FROM tblTMP > END > ELSE > PRINT 'IF False' > GO > > /*tblMISSINGTABLE does not exist and tblTMP.tmpID = 1 Returns: IF False, as expected*/ > IF ((SELECT [tmpID] FROM [dbo].[tblTMP]) = 0) > BEGIN > PRINT 'IF True' > SELECT tblMISSINGTABLE.lngMISSINGCOL FROM tblMISSINGTABLE > END > ELSE > PRINT 'IF False' > GO >
- Next message: Johannes Lebek: "JDBC batch error message without executeBatch() call"
- Previous message: Richard J: "RE: Latest entry..."
- In reply to: Donmn: "IF Statement causes Invalid Column Name"
- Next in thread: Donmn: "Re: IF Statement causes Invalid Column Name"
- Reply: Donmn: "Re: IF Statement causes Invalid Column Name"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|
|