In SQL Server 2008, I would like to add a column to the table and update it immediately after, but only if they have not been created before. I do not want to start the update if a column was previously created.
IF NOT EXISTS (SELECT *
FROM [INFORMATION_SCHEMA].[COLUMNS]
WHERE [TABLE_NAME] = 'SETTINGS' AND [COLUMN_NAME] = 'COLOR_SCHEME')
BEGIN
ALTER TABLE [SETTINGS]
ADD [COLOR_SCHEME] int NULL
UPDATE [SETTINGS]
SET [COLOR_SCHEME] = 1
END
The "GO" room after adding a column does not work, because it will not be a complete batch statement, but if I try to run it like this, I get the error "Invalid column name COLOR_SCHEME". "
Any ideas on how to make the column exist when the update starts?
source
share