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;fetch

相關文章
相關標籤/搜索