Skip to content

Instantly share code, notes, and snippets.

@kmoormann
Created September 20, 2012 20:47
Show Gist options
  • Save kmoormann/3758257 to your computer and use it in GitHub Desktop.
Save kmoormann/3758257 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
@kmoormann
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment