从另一个数据库查找标识列

发布于 2024-10-31 02:50:46 字数 613 浏览 1 评论 0原文

通常,使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

羁〃客ぐ 2024-11-07 02:50:46

Object_ID 仅适用于当前数据库,除非您使用由三部分组成的名称,但该形式使用起来很复杂。此外,ColumnProperty 仅适用于当前数据库。

select o.name + '.' + c.name, o.name
from test1.sys.columns c
join test1.sys.objects o on c.object_id = o.object_id
join test1.sys.schemas s on s.schema_id = o.schema_id
where s.name = 'dbo'
  and o.is_ms_shipped = 0 and o.type = 'U'
  and c.is_identity = 1
order by o.name

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.

select o.name + '.' + c.name, o.name
from test1.sys.columns c
join test1.sys.objects o on c.object_id = o.object_id
join test1.sys.schemas s on s.schema_id = o.schema_id
where s.name = 'dbo'
  and o.is_ms_shipped = 0 and o.type = 'U'
  and c.is_identity = 1
order by o.name
埋情葬爱 2024-11-07 02:50:46

无法借助 COLUMNPROPERTY 从另一个数据库获取信息。但有一个解决方法:

DECLARE @DatabaseName VARCHAR(MAX)
DECLARE @TableName VARCHAR(MAX)
DECLARE @SQL VARCHAR(MAX)

SET @DatabaseName = 'MyDatabase'
SET @TableName = 'MyTable'

SET @SQL = '
SELECT 
    C.TABLE_NAME,
    C.COLUMN_NAME,
    S.IS_IDENTITY
FROM ' + @DatabaseName + '.INFORMATION_SCHEMA.COLUMNS AS C
LEFT JOIN ' + @DatabaseName + '.SYS.COLUMNS AS S ON OBJECT_ID(''' + @DatabaseName + '.dbo.' + @TableName + ''') = S.OBJECT_ID AND C.COLUMN_NAME = S.NAME
WHERE S.IS_IDENTITY = 1'

EXEC(@SQL)

There is no way to get information with the help of COLUMNPROPERTY from another database. But there is on workaround:

DECLARE @DatabaseName VARCHAR(MAX)
DECLARE @TableName VARCHAR(MAX)
DECLARE @SQL VARCHAR(MAX)

SET @DatabaseName = 'MyDatabase'
SET @TableName = 'MyTable'

SET @SQL = '
SELECT 
    C.TABLE_NAME,
    C.COLUMN_NAME,
    S.IS_IDENTITY
FROM ' + @DatabaseName + '.INFORMATION_SCHEMA.COLUMNS AS C
LEFT JOIN ' + @DatabaseName + '.SYS.COLUMNS AS S ON OBJECT_ID(''' + @DatabaseName + '.dbo.' + @TableName + ''') = S.OBJECT_ID AND C.COLUMN_NAME = S.NAME
WHERE S.IS_IDENTITY = 1'

EXEC(@SQL)
梦行七里 2024-11-07 02:50:46

这对我使用特定的数据库有用:

USE <database_name>;
GO
SELECT SCHEMA_NAME(schema_id) AS schema_name
    , t.name AS table_name
    , c.name AS column_name
FROM sys.tables AS t
JOIN sys.identity_columns c ON t.object_id = c.object_id
ORDER BY schema_name, table_name;
GO

This worked for me using a specific database:

USE <database_name>;
GO
SELECT SCHEMA_NAME(schema_id) AS schema_name
    , t.name AS table_name
    , c.name AS column_name
FROM sys.tables AS t
JOIN sys.identity_columns c ON t.object_id = c.object_id
ORDER BY schema_name, table_name;
GO
去了角落 2024-11-07 02:50:46

在此示例中,我在“Database1”中构造了一个存储过程,它使用动态 SQL 从“Database2”中的表中检索列信息(使用驻留在“Database2”中的 [INFORMATION_SCHEMA].[COLUMNS] 系统视图):

ALTER PROCEDURE [Database1].[Schema1].[ColumnNames] @Database2 nvarchar(128), @Schema2 nvarchar(128), @Table2 nvarchar(128)
AS

BEGIN
    DECLARE @Sql nvarchar(1000)

    SET @Sql = 'SELECT [COLUMN_NAME], [ORDINAL_POSITION] FROM [' + @Database2 + '].[INFORMATION_SCHEMA].[COLUMNS]
                WHERE [TABLE_SCHEMA] = ''' + @Schema2 + ''' AND [TABLE_NAME] = ''' + @Table2 + ''''
    EXEC(@Sql)
END 

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"):

ALTER PROCEDURE [Database1].[Schema1].[ColumnNames] @Database2 nvarchar(128), @Schema2 nvarchar(128), @Table2 nvarchar(128)
AS

BEGIN
    DECLARE @Sql nvarchar(1000)

    SET @Sql = 'SELECT [COLUMN_NAME], [ORDINAL_POSITION] FROM [' + @Database2 + '].[INFORMATION_SCHEMA].[COLUMNS]
                WHERE [TABLE_SCHEMA] = ''' + @Schema2 + ''' AND [TABLE_NAME] = ''' + @Table2 + ''''
    EXEC(@Sql)
END 
你是我的挚爱i 2024-11-07 02:50:46

我正在使用 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 视图包含以下可能有用的字段:

  • object_id(如果您有多个表,则用于连接回相关表)具有相同的身份字段名称)
  • name(身份字段的名称)
  • column_id(表中列的顺序)
  • is_identity(告诉您这是否是身份字段)
  • seed_value(身份字段的初始值)
  • increment_value(每次插入时身份字段增加多少)

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:

  • object_id (used to join back to the table in question in case you have multiple tables with the same identity field name)
  • name (the name of the Identity field)
  • column_id (the order of the column in your table)
  • is_identity (tells you if this is an identity field)
  • seed_value (the initial value of the identity field)
  • increment_value (how much the identity field goes up with each insert)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文