欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-10-18 23:16:03
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 ......


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;