Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 513
  • Last Modified:

When the IF statement is ignored.

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
pborregg
Asked:
pborregg
  • 2
2 Solutions
 
Anthony PerkinsCommented:
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
 
DBAduck - Ben MillerPrincipal ConsultantCommented:
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
 
pborreggAuthor Commented:
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
 
DBAduck - Ben MillerPrincipal ConsultantCommented:
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now