I want to do this in code, not with ALT+F1.
Asked
Active
Viewed 3.8k times
47
-
See also [How to identify whether the table has identity column](http://stackoverflow.com/q/2871701) and [How do you determine what SQL Tables have an identity column programatically](http://stackoverflow.com/q/87747) – Michael Freidgeim Jun 10 '16 at 08:49
5 Answers
92
You can also do it this way:
select columnproperty(object_id('mytable'),'mycolumn','IsIdentity')
Returns 1 if it's an identity, 0 if not.

Blorgbeard
- 101,031
- 48
- 228
- 272
-
1Just in case your table is not in the default schema (dbo), you need to specify it in the table name `mySchema.myTable` – SeReGa Nov 02 '21 at 22:56
24
sp_help tablename
In the output look for something like this:
Identity Seed Increment Not For Replication
----------- ------- ------------ ----------------------
userid 15500 1 0

Patrick McElhaney
- 57,901
- 40
- 134
- 167
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

Luke Bennett
- 32,786
- 3
- 30
- 57
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'

Tschallacka
- 27,901
- 14
- 88
- 133
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:
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

KamalDeep
- 791
- 5
- 8