Solved

When the IF statement is ignored.

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

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

Suggested Solutions

Title # Comments Views Activity
SQL STANDARD CORE 8 40
Help With SQL Query 9 33
too many installs coming along with SQL 2016? 1 18
Need to find substring in SQL 5 19
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

820 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