Breaking News
Loading...
03/09/2013

Find a column in the same table in similar databases

In project, we have a template database that deployed for many countries, these database are put in the same SQL Server instance.

And today I need a T-SQL script to search each of the databases to check the data type of Description column in the DetailLog table.

Following is the script I wrote:

sp_msforeachdb 'select "?" AS db, t.name AS table_name,
SCHEMA_NAME(t.schema_id) AS schema_name,
c.name AS column_name, ty.name as Data_Type from [?].sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
JOIN sys.types AS ty ON c.user_type_id=ty.user_type_id
WHERE c.name LIKE ''%Description%'' and t.name=''DetailLog''
ORDER BY schema_name, table_name;'

And here is the result:

Refer: http://blog.sqlauthority.com/2009/04/26/sql-server-list-all-the-tables-for-all-databases-using-system-tables/

0 comments:

Post a Comment

 
Toggle Footer