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 primary key (id) '
print @strsql
exec (@strsql)
fetch next from tableNameCursor into @tablename
end
close tableNameCursor
deallocate tableNameCursor
12-11
2501
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)