I am trying to execute the following code as part of a migration in a transaction, but the code fails unless I put the GO statement after the ADD CONSTRAINT statement:
ALTER TABLE T ADD C INT NULL
ALTER TABLE T ADD CONSTRAINT DF_T_C DEFAULT ((1)) FOR C
GO
UPDATE T SET C = DEFAULT
ALTER TABLE T ALTER COLUMN C INT NOT NULL
If I leave out the GO statement I get the following error:
Invalid column name ‘C’.
The code executing the migration cannot handle the GO statement, how can I get this to work in a single transaction?
You can use
EXECfor the problematic statements so they get compiled as a different batch.But you can also do
Rather than doing all these individual steps yourself.