在 MSSQL 2000 中如何确定某列是否为标识列?

发布于 2024-07-07 18:24:18 字数 32 浏览 6 评论 0原文

我想在代码中执行此操作,而不是使用 ALT+F1。

I want to do this in code, not with ALT+F1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

荒人说梦 2024-07-14 18:24:19

在 Sql Server 2016 中,您可以通过查询 sys.all_columns 表来检查字段是否是同一字段:

SELECT * from sys.all_columns where object_id = OBJECT_ID('TableName') AND [Name] = 'ColumnName' AND is_identity = 1

In Sql Server 2016 you can check if a field is identity by querying against the sys.all_columns table:

SELECT * from sys.all_columns where object_id = OBJECT_ID('TableName') AND [Name] = 'ColumnName' AND is_identity = 1
傻比既视感 2024-07-14 18:24:18

您也可以这样做:

select columnproperty(object_id('mytable'),'mycolumn','IsIdentity')

如果它是一个恒等则返回 1,否则返回 0。

You can also do it this way:

select columnproperty(object_id('mytable'),'mycolumn','IsIdentity')

Returns 1 if it's an identity, 0 if not.

中二柚 2024-07-14 18:24:18
sp_help tablename 

在输出中查找类似以下内容:

 Identity     Seed     Increment     Not For Replication    
 -----------  -------  ------------  ---------------------- 
 userid       15500    1             0        
sp_help tablename 

In the output look for something like this:

 Identity     Seed     Increment     Not For Replication    
 -----------  -------  ------------  ---------------------- 
 userid       15500    1             0        
英雄似剑 2024-07-14 18:24:18

调整 WHERE 子句以适应:

select
    a.name as TableName,
    b.name as IdentityColumn
from
    sysobjects a inner join syscolumns b on a.id = b.id
where
    columnproperty(a.id, b.name, 'isIdentity') = 1
    and objectproperty(a.id, 'isTable') = 1

Adjust the WHERE clause to suit:

select
    a.name as TableName,
    b.name as IdentityColumn
from
    sysobjects a inner join syscolumns b on a.id = b.id
where
    columnproperty(a.id, b.name, 'isIdentity') = 1
    and objectproperty(a.id, 'isTable') = 1
终难愈 2024-07-14 18:24:18

作为 @Blogbeard 答案的扩展

如果您喜欢纯查询而不是内置函数

select col_name(sys.all_objects.object_id, column_id) as id from sys.identity_columns 
join sys.all_objects on sys.identity_columns.object_id = sys.all_objects.object_id
where sys.all_objects.name = 'system_files'

As expansion on @Blogbeard's answer

If you like pure query and not inbuilt functions

select col_name(sys.all_objects.object_id, column_id) as id from sys.identity_columns 
join sys.all_objects on sys.identity_columns.object_id = sys.all_objects.object_id
where sys.all_objects.name = 'system_files'
半枫 2024-07-14 18:24:18

Identity 是用于加载到表中的第一行的值。

有一篇微软文章可以提供有关身份的良好知识:

https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql-identity-property?view=sql-server-2017


现在,有几种方法可以识别表中哪一列是标识列:

  • 我们可以使用 sql 查询: select
    columnproperty(object_id('mytable'),'mycolumn','IsIdentity')
  • sp_help 表名

Identity is the value that is used for the very first row loaded into the table.

There is a microsoft article which can provide good knowledge about Identity:

https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql-identity-property?view=sql-server-2017

Now, there are couple of ways for identifying which column is an identity column in a table:

  • We can use sql query: select
    columnproperty(object_id('mytable'),'mycolumn','IsIdentity')
  • sp_help tablename
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文