Solved

When the IF statement is ignored.

Posted on 2013-01-03
4
462 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
  • 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now