sqlserver 主键检测

declare @tablename sysname
 
declare @strsql nchar(500)
 
declare tableNameCursor cursor for
 
select b.name from sysobjects b where xtype='U' and b.name not in
 
(select object_name(a.parent_obj) from sysobjects a where xtype='PK')


open tableNameCursor


fetch next from tableNameCursor into @tablename


while @@FETCH_STATUS = 0


begin


print @tablename


set @strsql= 'alter table [' + @tablename + '] add 主键 bigint identity(1,1);' + ';alter table ['+ @tablename+'] add constraint ['+ @tablename+'索引] primary key(主键)'


--use [{0}];alter table  [{1}] add 主键 bigint identity(1,1) ;
--alter table  [{1}] add constraint [{1}索引] primary key(主键)
print @strsql


exec (@strsql)


fetch next from tableNameCursor into @tablename


end


close tableNameCursor


deallocate tableNameCursor

你可能感兴趣的:(sqlserver 主键检测)