Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

When the IF statement is ignored.

Posted on 2013-01-03
4
Medium Priority
?
508 Views
Last Modified: 2013-01-04
Here we are with the BASIC IF statement

IF EXISTS( SELECT * FROM INFORMATION_SCHEMA.COLUMNS
            WHERE TABLE_NAME = 'TABLENAME'
                  AND COLUMN_NAME = 'COL1'
                  AND COLUMN_NAME = 'COL2')

      BEGIN                  
            PRINT 'They are present'
      END
      
ELSE
      BEGIN
            PRINT 'They do not exist'
      END


The above code works dandy and prints "THEY DO NOT EXIST"

BUT..... and it's a BIG BUT....

When I add this....

IF EXISTS( SELECT * FROM INFORMATION_SCHEMA.COLUMNS
            WHERE TABLE_NAME = 'TABLENAME'
                  AND COLUMN_NAME = 'COL1'
                  AND COLUMN_NAME = 'COL2')

      BEGIN                  

            PRINT 'They are present'

                        UPDATE      TABLENAME
                  SET      intCOLNEW = 1
                  WHERE COL1 = 0      AND COL2 = 0            

      END
      
ELSE
      BEGIN
            PRINT 'They do not exist'
      END


I get this....

Msg 207, Level 16, State 1, Line 8
Invalid column name 'COL1'

YES THE COLUMNS ARE GONE because of the previous run that deletes them. This is just a snippet of the problem.

The Procedure first checks the OLD columns (COL1 and COL2) and converts the data to a BIT Storing that value in the NEW column intCOLNEW as a 0 or 1

Then, once the new values are done, the OLD columns are deleted from the table.

Simple...

But when I update the Stored PROC, the problem is, the columns are no longer there, of course, and MS SQL Server BLOWS by the IF Statement and when it should go to the ELSE is tries to UPDATE again on columns that don't exist.

WHY????????????????????????????

Thank you....

Time crunch... so your quick and rapid assistance would be greatly appreciated.

Peter
0
Comment
Question by:pborregg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 1000 total points
ID: 38742573
I am not sure if you are looking for a workaround or a reason.  If it is just the reason, then that is simple:  You have a compile error not a run-time error.  For a workaround you may have to resort to using Dynamic SQL.
0
 
LVL 25

Assisted Solution

by:DBAduck - Ben Miller
DBAduck - Ben Miller earned 1000 total points
ID: 38742666
That is right.  Parsing is OK, because your syntax is correct.  But name resolution happens when it compiles and if the COL1 does not exist, it will not even get to the Runtime stage.

So like acperkins said, you would have to resort to dynamic SQL to get this to get past the compile stage.
0
 

Author Closing Comment

by:pborregg
ID: 38743507
OK, Thanks, that did it and it works!!!!!!!

for quibbles and quips...here's the revised SQL:

DECLARE @colName1 varchar(30)
DECLARE @colName2 varchar(30)
                  
SET @colName1 = 'SomeActualColumnName1'
SET @colName2 = 'SomeActualColumnName2'
      
IF EXISTS( SELECT * FROM INFORMATION_SCHEMA.COLUMNS
            WHERE TABLE_NAME = 'TableName'
                  AND COLUMN_NAME = @colName1
                  AND COLUMN_NAME = @colName2)
                  

      BEGIN      
                  
                  UPDATE      TableName
                  SET      intNewColumnName = 1
                  WHERE @colName1 = 0      AND @colName2 = 0                  

      END      
GO
0
 
LVL 25

Expert Comment

by:DBAduck - Ben Miller
ID: 38743719
That is not quite the dynamic SQL we were talking about.  This will only take what is in the variables @colName1 and @colName2 and evaluate whether or not they = 0.  So this would never be true if the value in @colName1 is 'COL1'.

You need to do something like:

DECLARE @sql nvarchar(4000)
SET @sql = N'UPDATE TableName SET intNewColumnName = 1 WHERE COL1 = 0 AND COL2 = 0'
EXEC (@sql)
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question