Skip to content

Instantly share code, notes, and snippets.

@davepcallan
Created May 7, 2024 16:20
Show Gist options
  • Save davepcallan/3a9f3a8e8460402606df8df172fb6ce7 to your computer and use it in GitHub Desktop.
Save davepcallan/3a9f3a8e8460402606df8df172fb6ce7 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