mssql sqlserver 检测一张表存在标识列的方法


摘要:
下文主要讲述检测数据表是否包含标识列的方法


方法1:

 declare @tbName varchar(30)
 set @tbName = '表名';---待检测表名
if exists(Select top 1 1 from sysobjects
           Where objectproperty(id, 'TableHasIdentity') = 1
             and upper(name) = upper(@tbName)
         )
     begin
	  print '表:'+@tbName+' 存在标识列!'
	 end
    else
	begin
	  print '表:'+@tbName+' 不存在标识列!'
	end

----------------方法2--------------------
  declare @tbName varchar(30)
 set @tbName = 'A'; ---待检测表名 
if exists(Select top 1 1 from sysobjects a
           Where a.xtype = 'U'
             and upper(a.name) = upper(@tbName)
             and exists(Select Top 1 1 from syscolumns b
                         Where b.id = a.id
                           and columnproperty(b.id, b.name, 'IsIdentity') = 1
                       )
         ) 
     begin
	  print '表:'+@tbName+' 存在标识列!'
	 end
    else
	begin
	  print '表:'+@tbName+' 不存在标识列!'
	end