Solved

When the IF statement is ignored.

Posted on 2013-01-03
4
503 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 250 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 24

Assisted Solution

by:DBAduck - Ben Miller
DBAduck - Ben Miller earned 250 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 24

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

626 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