EXEC sp_pkeys @table_name='表名'
2:利用系统表INFORMATION_SCHEMA.KEY_COLUMN_USAGE
SELECT TABLE_NAME,COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME='表名'
关于SQL SERVER自增列
判断Table是否存在自增列(Identity column):
declare @Table_name varchar(60)
set @Table_name = '';
if Exists(Select top 1 1 from sysobjects
Where objectproperty(id, 'TableHasIdentity') = 1
and upper(name) = upper(@Table_name)
)
select 1
else select 0
-- or
if Exists(Select top 1 1 from sysobjects so
Where so.xtype = 'U'
and upper(so.name) = upper(@Table_name)
and Exists(Select Top 1 1 from syscolumns sc
Where sc.id = so.id
and columnproperty(sc.id, sc.name, 'IsIdentity') = 1
)
)
select 1
else select 0
判断Table是否存在自增列(Identity column),并查出自增列相关数据:
declare @Table_name varchar(60)
set @Table_name = 'user';
Select so.name Table_name, --表名字
sc.name Iden_Column_name, --自增字段名字
ident_current(so.name) curr_value, --自增字段当前值
ident_incr(so.name) incr_value, --自增字段增长值
ident_seed(so.name) seed_value --自增字段种子值
from sysobjects so
Inner Join syscolumns sc
on so.id = sc.id
and columnproperty(sc.id, sc.name, 'IsIdentity') = 1
Where upper(so.name) = upper(@Table_name)