+ -
当前位置:首页 → 问答吧 → 把非自增的字段转换成自增字段

把非自增的字段转换成自增字段

时间:2011-11-15

来源:互联网

有大量的表要转换,把非自增字段转为自增,并加主键

有没有这样的存储过程啊?

作者: aspwebchh   发布时间: 2011-11-15

没有
可用动态生成语句

如:
SQL code

--删除主健
select 'alter table '+quotename(a.name)+' drop constraint ' +QUOTENAME(b.Name)
from sys.objects as a
    inner join  sys.objects as b on a.object_id=b.parent_object_id and b.type='PK'
where 
not exists(select 1 from sys.columns where object_id=a.object_id and is_identity=1) 
and a.type='U'

--新增标识列为主健,列名为ID,首先要定义好列名的唯1
select 'alter table '+quotename(name)+' add ID int identity(1,1) primary key' 
from sys.objects as a 
where 
not exists(select 1 from sys.columns where object_id=a.object_id and is_identity=1) 
and type='U'



作者: roy_88   发布时间: 2011-11-15