How do I determine if a column is an identity column in MSSQL 2000?
Asked Answered
K

6

47

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

Kaiser answered 9/10, 2008 at 20:1 Comment(1)
R
94

You can also do it this way:

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

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

Resuscitator answered 9/10, 2008 at 20:16 Comment(1)
Just in case your table is not in the default schema (dbo), you need to specify it in the table name mySchema.myTableShantae
F
25
sp_help tablename 

In the output look for something like this:

 Identity     Seed     Increment     Not For Replication    
 -----------  -------  ------------  ---------------------- 
 userid       15500    1             0        
Favian answered 9/10, 2008 at 20:5 Comment(0)
A
7

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
Alfeus answered 9/10, 2008 at 20:15 Comment(0)
S
1

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'
Scorper answered 26/7, 2016 at 13:51 Comment(0)
H
1

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
Horner answered 3/11, 2018 at 20:15 Comment(0)
E
0

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
Electrodynamic answered 13/2 at 16:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.