如何根据其架构和表名获取表列?

时间:2014-07-17 06:54:45

标签: sql-server sql-server-2008

我希望针对特定ColumnNamedatatype的不同数据库的每一列获取is_identity及其tableschema。< / p>

我使用以下代码,但SCHEMA_NAME用于当前数据库,但我想从master or another database运行此查询。

如何检查架构名称?

SELECT  
   c.name AS column_name, tp.name as data_type, c.is_identity iden 
FROM 
   DatabaseName.sys.tables AS t 
INNER JOIN 
   DatabaseName.sys.columns c ON t.OBJECT_ID = c.OBJECT_ID 
INNER JOIN 
   DatabaseName.sys.types tp ON c.user_type_id =  tp.user_type_id  
WHERE
   t.name = 'PlaceType' 
   AND SCHEMA_NAME(t.schema_id) = 'Coding'

2 个答案:

答案 0 :(得分:1)

而不是SCHEMA_NAME,请使用OBJECT_SCHEMA_NAME,如下所示:

SELECT  c.name AS column_name,tp.name as data_type,c.is_identity iden FROM 
DatabaseName.sys.tables AS t INNER JOIN 
DatabaseName.sys.columns c ON t.OBJECT_ID = c.OBJECT_ID INNER JOIN 
DatabaseName.sys.types tp ON c.user_type_id =  tp.user_type_id  where 
t.name='PlaceType' and OBJECT_SCHEMA_NAME(t.object_id,DB_ID(Databasename))='Coding'

答案 1 :(得分:0)

只需更改最后一个条件,如

and SCHEMA_NAME(t.schema_id) in (Select name from Sys.Databases where database_id > 4)