Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save disouzam/3310202db69cfd28ea580ff5c06020dc to your computer and use it in GitHub Desktop.
Save disouzam/3310202db69cfd28ea580ff5c06020dc to your computer and use it in GitHub Desktop.
Add created_date column to all tables which don't have it already in SQL Server
SELECT 'ALTER TABLE ' + QUOTENAME(ss.name) + '.' + QUOTENAME(st.name) + ' ADD created_date DATETIME NULL;'
FROM sys.tables st
INNER JOIN sys.schemas ss on st.[schema_id] = ss.[schema_id]
WHERE st.is_ms_shipped = 0
AND NOT EXISTS (
SELECT 1
FROM sys.columns sc
WHERE sc.[object_id] = st.[object_id]
AND sc.name = 'created_date'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment