Friday, September 21, 2007

How to find identiy column of the tables

If you want to know identity columns of the tables of SQL Server, you can use this query;

select o.name as TableName, c.name as ColumnName
from sys.columns c, sys.objects o, sys.schemas s
where c.object_id = o.object_id
and o.schema_id = s.schema_id
and c.is_identity = 1
and o.type_desc = 'USER_TABLE'
and s.name = 'dbo'
order by o.name


This query is usefull when migrating the database from old one. This query search all the user tables not system tables.