我希望针对特定ColumnName
和datatype
的不同数据库的每一列获取is_identity
及其table
和schema
。< / 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'
答案 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)