IF NOT EXISTS(SELECT 1 FROM sys.columns
WHERE Name = N'MyColumn' AND Object_ID = Object_ID(N'[MySchema].[MyTable]'))
ALTER TABLE [MySchema].[MyTable]
ADD [MyColumn] VARCHAR(10) NULL
GO
UPDATE [MySchema].[MyTable]
SET [MyColumn] = 'ValueForExistingData'
WHERE [MyColumn] IS NULL
ALTER TABLE [MySchema].[MyTable]
ALTER COLUMN [MyColumn] VARCHAR(10) NOT NULL
What kind of locking / transaction isolation level is required to ensure that no new rows are inserted in between the UPDATE and ALTER COLUMN statements? This is to prevent the ALTER COLUMN statement from failing because of a NULL value in MyColumn in the newly inserted row.
Is there a better alternative to GO to ensure ADD [MyColumn] completes before SET [MyColumn] ?