Quantcast
Viewing all articles
Browse latest Browse all 40

Microsoft SQL Server – Search specified column or table in all database at a DB Server

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%'';'

Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 40

Trending Articles