SQL SERVER 使用游标删除所有主键
程序员文章站
2022-06-03 19:21:15
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;
推荐阅读
-
MS SQL Server游标(CURSOR)的学习使用
-
在SQL Server中使用SQL语句查询一个存储过程被其它所有的存储过程引用的存储过程名
-
SQL Server设置主键自增长列(使用sql语句实现)
-
SQL Server遍历表中记录的2种方法(使用表变量和游标)
-
SQL Server游标的使用/关闭/释放/优化小结
-
使用SQL Server判断文件是否存在后再删除(详解)
-
SQL SERVER 使用游标删除所有主键
-
MS SQL Server游标(CURSOR)的学习使用
-
SQL Server使用游标处理Tempdb究极竞争-DBA问题-程序员必知
-
Sql Server临时表和游标的使用小结