I'm working on a DB upgrade script that needs to drop a column (that part's easy). Before I can drop the column, I want to take the value and move it into another column that already exists.
I'm trying to acheive idempotence with my script, which is where I seem to be failing. I have code like this...
IF EXISTS (SELECT *
FROM sys.all_columns
WHERE sys.all_columns.object_id = OBJECT_ID(N'[dbo].[MyTable]')
AND sys.all_columns.name = 'Column1')
BEGIN
UPDATE [dbo].[MyTable]
SET [Column2] = [Column1]
ALTER TABLE [dbo].[MyTable]
DROP COLUMN [Column1]
END
No matter what I do, the UPDATE
query is always executed, even if the condition is false, which causes errors. What do I need to do to prevent this query from being run if my initial condition is false? The ALTER TABLE
statement below it only gets run if the condition is true.