Tags

,

In a development environment normally there are multiple databases installed at one DB Server, and there is a system store procedure called: “sp_msforeachdb” can help us to search a specified column or table easily in these different databases(or other tasks need to be done in different DBs), and here are the sample script:

Search a specified column name in all database at a DB Server

-- Search a specified column name in all database at a DB Server
sp_msforeachdb
'SELECT
''[''+ "?" +''].[''+SCHEMA_NAME(schema_id)+''].[''+t.name+'']''
as "Full table name",
"?" AS DBName,
SCHEMA_NAME(schema_id) AS Schema_Name,
t.name AS Table_Name,
col.name AS Column_Name
FROM [?].sys.tables AS t
INNER JOIN [?].sys.columns col ON t.OBJECT_ID = col.OBJECT_ID
WHERE col.name like ''%ColumnName_search_keyword%''
ORDER BY schema_name, table_name; '

Search a specified table name in all databases at a DB Server

-- Search a specified table name in all databases at a DB Server
sp_msforeachdb
'SELECT
''[''+ "?" +''].[''+SCHEMA_NAME(schema_id)+''].[''+t.name+'']''
as "Full Table Name",
"?" AS DBName,
SCHEMA_NAME(schema_id) AS Schema_Name,
t.name AS Table_Name
FROM [?].sys.tables AS t
WHERE t.name like ''%tablename_search_keyword%'';'
Advertisements