判断表中是否存在SQL自增列

如何判断表中是否存在SQL自增列呢?这是很多人都提到过的问题,下面就为您介绍判断表中是否存在SQL自增列的实现方法,供您参考。

判断Table是否存在SQL自增列(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 = '';

 

 

 

【编辑推荐】

SQL SELECT语句的使用

SQL Server中select into语法详解

使用SQL Delete命令删除记录

SQL Server创建表和删除表

SQL Server存储过程的单步调试

THE END