sql 表有没有自增列,插入自增列值

创建一个表

create table demoTb
(
	ID int identity(1,1) primary key,
	name varchar(30)
) 

  查看有没有自增列  有就返回YES

IF ((SELECT OBJECTPROPERTY( OBJECT_ID(N'demoTb'), 'TableHasIdentity')) = 1)
    PRINT 'Yes'
ELSE
    PRINT 'No'

  如果想插入自增列的值就

declare @tableName varchar(250)
declare @sql nvarchar(1000)
select @tableName = 'demoTb'
select @sql ='SET IDENTITY_INSERT  '+@tableName+' ON' +char(13)+'insert into demoTb(id,name) values(2,''wu'')'
print @sql
exec sp_executesql  @sql
insert into demoTb(id,name) values(1,'wu')
select  * from demoTb

  

posted @ 2013-01-11 10:11  li-peng  阅读(6439)  评论(0编辑  收藏  举报