从另一个数据库查找标识列
通常,使用 SQL Server,您可以使用像这样的 COLUMNPROPERTY 函数来查找数据库中的标识列:
select TABLE_NAME + '.' + COLUMN_NAME, TABLE_NAME
from INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA = 'dbo'
and COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1
order by TABLE_NAME
但我不知道如何在从另一个数据库运行查询时使其工作。例如,这不会返回任何结果:
Use FirstDatabase
Go
select TABLE_NAME + '.' + COLUMN_NAME, TABLE_NAME
from SecondDatabase.INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA = 'dbo'
and COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1
order by TABLE_NAME
Normally with SQL Server you can use the COLUMNPROPERTY
function like this to find the Identity columns in a database:
select TABLE_NAME + '.' + COLUMN_NAME, TABLE_NAME
from INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA = 'dbo'
and COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1
order by TABLE_NAME
But I can't figure out how to get this to work when running the query from another database. E.g. this does not return any results:
Use FirstDatabase
Go
select TABLE_NAME + '.' + COLUMN_NAME, TABLE_NAME
from SecondDatabase.INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA = 'dbo'
and COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1
order by TABLE_NAME
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Object_ID 仅适用于当前数据库,除非您使用由三部分组成的名称,但该形式使用起来很复杂。此外,ColumnProperty 仅适用于当前数据库。
Object_ID only works in current db, unless you use a 3-part name, but that form is complicated to use. Also, ColumnProperty only works in current db.
无法借助 COLUMNPROPERTY 从另一个数据库获取信息。但有一个解决方法:
There is no way to get information with the help of COLUMNPROPERTY from another database. But there is on workaround:
这对我使用特定的数据库有用:
This worked for me using a specific database:
在此示例中,我在“Database1”中构造了一个存储过程,它使用动态 SQL 从“Database2”中的表中检索列信息(使用驻留在“Database2”中的 [INFORMATION_SCHEMA].[COLUMNS] 系统视图):
In this example, I've constructed a stored procedure in "Database1" that uses dynamic SQL to retrieve column information from a table in "Database2" (using the [INFORMATION_SCHEMA].[COLUMNS] system view residing in "Database2"):
我正在使用 SQL Server 2019,并且遇到了同样的挑战。我不确定此修复是否适用于旧版本,但每个数据库中都有一个名为 Your-DB-Name.sys.identity_columns 的视图。如果您从此视图中进行选择,您将看到您在该数据库中定义的标识列的列表。
根据该信息,您应该能够编写连接 YourDBName.Information_schema.columns 的联接,如下所示:
SELECT *
FROM YourDBName.Information_Schema.columns col
左外连接 YourDBName.sys.identity_columns idc
ON idc.name = col.COLUMN_NAME AND idc.object_id = object_id('YourDBName..YourTableName')
WHERE col.TABLE_NAME = 'YourTableName' AND col.table_catalog = 'YourDBName';
YourDbName.sys.identity_columns 视图包含以下可能有用的字段:
I'm using SQL Server 2019, and I have run into the same challenge. I'm not sure if this fix will work for older versions, but there is a view in each DB called Your-DB-Name.sys.identity_columns. If you select from this view, you'll see the list of identity columns you have defined in that DB.
From that information you should be able to write a join connecting YourDBName.Information_schema.columns such as below:
SELECT *
FROM YourDBName.Information_Schema.columns col
LEFT OUTER JOIN YourDBName.sys.identity_columns idc
ON idc.name = col.COLUMN_NAME AND idc.object_id = object_id('YourDBName..YourTableName')
WHERE col.TABLE_NAME = 'YourTableName' AND col.table_catalog = 'YourDBName';
The YourDbName.sys.identity_columns view contains the following fields that might be useful: