2

We have a script that must allow for being re-run several times.

We have an MS-SQL script that updates a table if a (now obsolete) column exists, then deletes the column. To ensure that the script can be run several times, it first checks for the existence of a column before performing the updates.

The script works as expected on our dev database, updating the data on the first run, then displaying the message 'Not updating' on subsequent runs.

On our test database the script runs fine on the first run, but errors with "Invalid column name 'OldColumn'" on subsequent runs; if I comment out the UPDATE and ALTER statements it runs as expected.

Is there a way to force the script to run even if there's a potential error, or is it something to do with how the database was set-up? (fingers crossed I'm not looking like a complete noob!)

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MyTable' AND COLUMN_NAME = 'OldColumn')

    BEGIN
        PRINT 'Updating and removing old column...'
        UPDATE MyTable SET NewColumn='X' WHERE OldColumn=1;
        ALTER TABLE MyTable DROP COLUMN OldColumn;
    END

ELSE
    PRINT 'Not updating'
GO
3
  • 2
    It's dropping a column and you can't figure out why it's not updating the column you dropped? Commented Mar 31, 2011 at 0:10
  • @Randolph - That isn't the OP's question at all. On subsequent runs when the column doesn't exist that code doesn't get executed. They are asking why the non executed code causes a compilation problem in one environment but not another. Commented Mar 31, 2011 at 0:45
  • Right, I'm with you. Sorry for the confusion. Commented Mar 31, 2011 at 4:29

1 Answer 1

1

As a work around you could do

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MyTable' AND COLUMN_NAME = 'OldColumn')
    BEGIN
        PRINT 'Updating and removing old column...'
        EXEC ('UPDATE MyTable SET NewColumn=''X'' WHERE OldColumn=1;');
        ALTER TABLE MyTable DROP COLUMN OldColumn;
    END

ELSE
    PRINT 'Not updating'
Sign up to request clarification or add additional context in comments.

7 Comments

exec sp_dbcmptlevel 'dbname' displays 'The current compatibility level is 100.' for both databases
@cooman - Yes I just began to doubt the answer myself. Compilation of the statement should only be deferred if it refers to a table that does not exist. Does the table itself definitely still exist in the dev environment?
@Martin - the table is definitely there; if I change the update to "UPDATE MyTable SET NewColumn='X';" and remove the DROP it correctly displays the 'Not updating' message
That doesn't mean the table is still there. Compilation would be deferred and you would never actually enter the if block if it wasn't. I get the same error as you on my machine unless I wrap it in dynamic SQL so it executes in another batch. This is how it should work (ie is documented to work) according to my understanding of it as well. If the table is absolutely definitely there in your dev environment would be interesting to know @@version for both.
I've verified the table is there (I also copied the UPDATE into the ELSE to be sure) but now dev and test both error :( I luckily have other databases to test against, and I've found that changing the compatibility level to 90 reproduces the error, but resetting back to 100 doesn't put it all back as it was... I still get the error. Perhaps I should just convert to dynamic SQL (which I've tried and works as expected)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.