Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

关于删除SQL Server一些对象的SQL语句

$
0
0

在我们学习工作生活,可能需要将数据对象批量的删除;

以下SQL语句,可能会帮到我们,在SQL Server 2000/2005 测试通过;

declare @objname as varchar(100)
declare @subobject as varchar(100)
declare @sql as varchar(300)

--delete all constraints or foreign keys
while exists (select a.[name] as tablename ,b.[name] as objname 
			  from sysobjects a,(select [name],parent_obj from sysobjects where [type]='D' or [type]= 'F') b
			  where a.id = b.parent_obj)
begin
select top 1 @objname  = a.[name] ,@subobject = b.[name]
			  from sysobjects a,(select [name],parent_obj from sysobjects where [type]='D' or [type]= 'F') b
			  where a.id = b.parent_obj
select @sql = 'alter table ' + @objname + ' drop constraint ' + @subobject
exec(@sql) 
end 

--delete all views 
while exists (select * from sysobjects where [type]='V' and id > 0)
begin
select top 1 @objname = [name] from sysobjects where [type]='V' and id > 0
select @sql = 'drop view ' + @objname
exec(@sql)
end 

--delete all procedures
while exists (select * from sysobjects where [type]='P' and id >0)
begin
select top 1 @objname = [name] from sysobjects where [type]='P' and id >0
select @sql = 'drop proc ' + @objname
exec(@sql)
end 


--delete all tables
while exists (select * from sysobjects where [type]='U')
begin
select top 1 @objname = [name] from sysobjects where [type]='U'
select @sql = 'drop table ' + @objname
exec(@sql)
end 



作者:Boren31 发表于2013-11-19 8:30:32 原文链接
阅读:72 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>