Skip to content

Instantly share code, notes, and snippets.

@thebirk
Created September 27, 2024 08:47
Show Gist options
  • Save thebirk/8c208dcc7ed0319043c05c5d3520f148 to your computer and use it in GitHub Desktop.
Save thebirk/8c208dcc7ed0319043c05c5d3520f148 to your computer and use it in GitHub Desktop.
Check if column exists in a table across all databases - SQL Server
DECLARE @cmd nvarchar(2000)
SET @cmd = N'USE [?] IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = ''TABLE_NAME_HERE''
AND COLUMN_NAME = ''COLUMN_NAME_HERE'')
BEGIN
SELECT DB_NAME() AS ''Database'', ''TRUE'' AS ''Exists''
END
ELSE SELECT DB_NAME() AS ''Database'', ''FALSE'' AS ''Exists'''
EXEC sp_MSforeachdb @command1 = @cmd;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment