Created
September 27, 2024 08:47
-
-
Save thebirk/8c208dcc7ed0319043c05c5d3520f148 to your computer and use it in GitHub Desktop.
Check if column exists in a table across all databases - SQL Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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