我想在SQL Server 2005中创建一个列列表,其中包含标识列及其在T-SQL中的相应表.
结果将是这样的:
TableName,ColumnName
对SQL Server执行此操作的另一种可能方法是使用INFORMATION_SCHEMA视图,该方法对系统表(可能发生更改,版本更换)的依赖性较小:
select COLUMN_NAME, TABLE_NAME from INFORMATION_SCHEMA.COLUMNS where COLUMNPROPERTY(object_id(TABLE_SCHEMA+'.'+TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1 order by TABLE_NAME
sys.columns.is_identity = 1
例如,
select o.name, c.name from sys.objects o inner join sys.columns c on o.object_id = c.object_id where c.is_identity = 1
另一种方式(2000/2005/2012/2014):
IF ((SELECT OBJECTPROPERTY( OBJECT_ID(N'table_name_here'), 'TableHasIdentity')) = 1) PRINT 'Yes' ELSE PRINT 'No'
注意:table_name_here
应该是schema.table
,除非架构是dbo
.
在SQL 2005中:
select object_name(object_id), name from sys.columns where is_identity = 1