当前位置: 代码迷 >> Sql Server >> SQL怎么直接批量删除表
  详细解决方案

SQL怎么直接批量删除表

热度:28   发布时间:2016-04-24 09:54:30.0
SQL如何直接批量删除表
将数据导入到了默认系统数据库master,现想删除,不能批量操作,请教。

------解决思路----------------------
SELECT 'drop table '+name+';' FROM sys.tables

------解决思路----------------------
--删除外键约束
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 
--删除表
DECLARE c2 cursor for 
    select 'drop table ['+name +']; '
    from sysobjects 
    where xtype = 'u' 
open c2
declare @c2 varchar(8000)
fetch next from c2 into @c2
while(@@fetch_status=0)
    begin
        exec(@c2)
        fetch next from c2 into @c2
    end
close c2
deallocate c2

------解决思路----------------------
引用:
SELECT 'drop table '+name+';' FROM sys.tables

至少得限定下是用户表。
------解决思路----------------------
1、使用下面的语句

select 'drop table '+name+';' from sys.objects where type ='u'


2、将查询的结果再执行即可
------解决思路----------------------
sys.tables就是用户表

引用:
Quote: 引用:

SELECT 'drop table '+name+';' FROM sys.tables

至少得限定下是用户表。

------解决思路----------------------
看样子像2008,SSMS里默认F7键是对象资源管理器,进入到表那一级,选中你要删除的表名,右键选删除,确认就行了
  相关解决方案