Sometimes you might need to find all your MS SQL database tables by a column name.
This could be because you might want to investigate a database error and here is how you can do it - all credit goes to AdaTheDev on StackOverflow;
Error
SQL Script to Find All Tables by a Column Name
--Search Tables
SELECT COLUMN_NAME AS 'ColumnName'
,TABLE_NAME AS 'TableName'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%preventCleanup%'
ORDER BY
TableName
,ColumnName;
--Search Tables and Views
SELECT c.name
AS 'ColumnName'
,t.name AS 'TableName'
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name LIKE '%preventCleanup%'
ORDER BY
TableName
,ColumnName;
Comments
Post a Comment