Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save iho/e1ef71e2b9ca785f5a3a0a73c807527b to your computer and use it in GitHub Desktop.
Save iho/e1ef71e2b9ca785f5a3a0a73c807527b to your computer and use it in GitHub Desktop.
Idempotent SQL Alter Table Statements
IF NOT EXISTS
(
SELECT * FROM [information_schema].[columns]
WHERE table_name = 'Customer'
AND table_schema = 'dbo'
AND column_name = 'FavoriteColorId'
)
BEGIN
ALTER TABLE [dbo].[Customer]
ADD FavoriteColorId int
END
IF EXISTS
(
SELECT * FROM [information_schema].[columns]
WHERE table_name = 'Customer'
AND table_schema = 'dbo'
AND column_name = 'FavoriteColorId'
)
BEGIN
ALTER TABLE [dbo].[Customer]
DROP COLUMN FavoriteColorId
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment