博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
批量删除Sql Server对象(表,存储过程,触发器)
阅读量:6908 次
发布时间:2019-06-27

本文共 953 字,大约阅读时间需要 3 分钟。

先在系统表中找到要处理的表名或者是存储过程的名字,在用游标对其进行处理

PS:SqlServer 2000使用的是系统表是sysobjects,类型字段是:xtype; SqlServer 2005以上版本的系统表是Sys.Objects,类型字段是Type

本文中以Sql2005为例,Sql2000版本请自行按照上述说明进行替换

注意  sys.objects 中type的值不同 删除命令是不同的

如删除存储过程用drop PROCEDURE PROCEDURENAME 删除表用 drop table  tablename 删除触发器用Drop Trigger TriggerName

sys.objects.type的值表示的意思如下表:

C:检查约束。
D:默认的约束
F:外键约束
L:日志
P:存储过程
PK:主键约束
RF:复制过滤存储过程
S:系统表格
TR:触发器
U:用于表格。
UQ:独特的约束。

批量处理的代码如下:

DECLARE cursorname cursor for select 'drop PROCEDURE  '+name from sys.objects where name like 'xx%' and xtype = 'p' --删除对应的存储过程
DECLARE cursorname cursor for select 'drop Trigger'+name from sys.objects where name like 'xx%' and xtype = 'tr' --删除对应的触发器

open cursorname

declare @curname sysname
fetch next from cursorname into @curname
while(@@fetch_status=0)
  begin
 exec(@curname)
fetch next from cursorname into @curname
end
close cursorname
deallocate cursorname 

转载于:https://www.cnblogs.com/duguqing40/archive/2012/05/07/2487139.html

你可能感兴趣的文章
创建线程的方式
查看>>
微软WP7开发者体验部门主管跳槽亚马逊
查看>>
OSGi规范概要
查看>>
关系数据库的末日是否已经来临(转载)
查看>>
第二人生 我的成长
查看>>
硬编码写RadioGroup的时候要注意RadioButton的id重复问题
查看>>
周三 全身心的工作
查看>>
angularJS 1.0.6 tutorial 对应的github commit
查看>>
ldap的搭建
查看>>
利用percona-toolkit工具检查MySQL数据库主从一致性并修复
查看>>
Filter-全站GZIP压缩
查看>>
Windows自带的文件加密方法揭秘
查看>>
单页应用开发
查看>>
【Scrapy学习】 scrapyd 文件配置
查看>>
作业-week-2
查看>>
我的友情链接
查看>>
TCP链接
查看>>
Exchange Server 2007迁移Exchange Server 2010 (2) ---前期准备之二
查看>>
翻译:Fast dynamic extracted honeypots in cloud computing --5.CONCLUSION
查看>>
Effective C++: constexpr(during compilation).
查看>>