SQL SERVER 使用游标删除所有主键


Declare @Pk varChar(100);
Declare @TBname varChar(100);
declare cursor1 cursor for
Select Sys2.name as TBname,Sys1.name as Pk from sysobjects Sys1 JOIN sysobjects Sys2 ON Sys1.parent_obj = Sys2.[id] where Sys1.xtype='PK';
open cursor1
fetch next from cursor1 into @TBname,@Pk
while @@fetch_status=0
begin
--exec('Alter table '+@TBname+' Drop '+ @Pk) --删除原主键
--print 'Alter table '+@TBname+' Drop '+ @Pk   --打印
fetch next from cursor1 into @TBname,@Pk
end
close cursor1 ;
deallocate cursor1;

posted @ 2019-07-15 13:27  AndSir  阅读(645)  评论(0编辑  收藏  举报
http://www.andsir.cn