加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 数据库 > MsSql > 正文

Sql Server 删除所有表实现方法

发布时间:2020-05-23 20:13:41 所属栏目:MsSql 来源:互联网
导读:Sql Server 删除所有表实现方法

1)首先必须要清空所有表的外键 SQL代码如下:


DECLARE c1 cursor for
select 'alter table ['+ object_name(parent_obj) + '] drop constraint ['+name+']; '
from sysobjects
where xtype = 'F'
open c1
declare @c1 varchar(8000)
fetch next from c1 into @c1
while(@@fetch_status=0)
begin
exec(@c1)
fetch next from c1 into @c1
end
close c1
deallocate c1

---- 来自jb51.cc 
2)第二部删除所有的表
SQL代码如下:

--清空数据库内所有的表 --注意替换数据库
use aqfk_2016_test
GO
declare @sql varchar(8000)
while (select count(*) from sysobjects where type='U')>0
begin
SELECT @sql='drop table ' + name
FROM sysobjects
WHERE (type = 'U')
ORDER BY 'drop table ' + name
exec(@sql)
end


---- 来自jb51.cc 
3)批量删除所有视图:
SQL代码如下:

select identity(int,1,1) flag,[name] names into #tmp
from sysobjects where xtype='v'
 
declare @tb varchar(1000),@a int,@b int,@sql varchar(8000)
select @a=min(flag),@b=max(flag) from #tmp
while @a<=@b
begin
select @tb=names from #tmp where flag=@a
set @sql='drop view "'+@tb+'"'
exec(@sql)
set @a=@a+1
end
DROP TABLE #tmp


---- 来自jb51.cc 

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读