sql server 2008 怎么批量删除表格

飞起来一脚 2010-01-08 09:48:42
以前2000下,选中表格,一个delete就删除了
08下不能多选,怎么一下删除多个表呢?
指令删除也行,假设这些表格的前缀为"temptb_"
请教大师们出马。
...全文
1169 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zlxiao001 2010-11-19
  • 打赏
  • 举报
回复
不错,在2005下是可以使用的
andysun88 2010-01-08
  • 打赏
  • 举报
回复

8楼的方法 最接近,但是有2个问题
1.就是你的 ‘like 'temptb_%';’ 中下划线_没有转义字符,如果有一个‘temptb789’的表,你一样能删除
2.就是你的 sys.tables 中如果有系统表,他也一样要删除,例如‘数据库中系统表中有个dbo.dtproperties’的,修改就是加上一个判断 is_ms_shipped =0,这样就是指的用户表。



借鉴8楼修改如下:
-- 测试数据
create table dbo.temptb_1 (id int);
create table guest.temptb_2 (id int);

-- 打开隐式事务
SET IMPLICIT_TRANSACTIONS ON
-- 执行动态删除
declare @sql varchar(max);
set @sql='';
select @sql=@sql+'drop table '+
QUOTENAME(SCHEMA_NAME([schema_id]))+'.'+QUOTENAME([name])+';'
from sys.tables where where is_ms_shipped =0 and [name] like 'temptb\_%' escape '\'
--select @sql;
EXEC(@sql);
-- 检查删除是否正确
select * from sys.tables
-- 正确,则提交事务,确认删除
commit tran
-- 不正确,则回滚事务,取消删除
rollback tran
-- 关闭隐式事务
SET IMPLICIT_TRANSACTIONS OFF

xman_78tom 2010-01-08
  • 打赏
  • 举报
回复

-- 测试数据
create table dbo.temptb_1 (id int);
create table guest.temptb_2 (id int);

-- 打开隐式事务
SET IMPLICIT_TRANSACTIONS ON
-- 执行动态删除
declare @sql varchar(max);
set @sql='';
select @sql=@sql+'drop table '+
QUOTENAME(SCHEMA_NAME([schema_id]))+'.'+QUOTENAME([name])+';'
from sys.tables where [name] like 'temptb_%';
--select @sql;
EXEC(@sql);
-- 检查删除是否正确
select * from sys.tables
-- 正确,则提交事务,确认删除
commit tran
-- 不正确,则回滚事务,取消删除
rollback tran
-- 关闭隐式事务
SET IMPLICIT_TRANSACTIONS OFF
飞起来一脚 2010-01-08
  • 打赏
  • 举报
回复
还是不行啊
我的其他用户表都是dbo.tabname的
但是那几个是username.temptb_***的形式,用上述提示执行成功,刷新后,表依旧在。
另外:系统表居然是空的,一个表一个没有,其他的数据库中系统表中有个dbo.dtproperties
andysun88 2010-01-08
  • 打赏
  • 举报
回复



select * from sys.tables where is_ms_shipped =0 and name like 'temptb\_%' escape '\'

还是不能删除,不允许对系统目录进行即席更新。

大家怎么没有换转义字符啊?'temptb\_%' escape '\'
lrjt1980 2010-01-08
  • 打赏
  • 举报
回复
drop from 表1,表2,......
chuifengde 2010-01-08
  • 打赏
  • 举报
回复
exec sp_msforeachtable 'drop table ?',@whereand=' and name like ''temptb_%'''
htl258_Tony 2010-01-08
  • 打赏
  • 举报
回复
SQL批量删除用户表(先删除所有外键约束,再删除所有表) 
--删除外键约束
DECLARE c1 cursor for
select 'alter table ['+ object_name(parent_obj) + '] drop constraint ['+name+']; '
from sysobjects
where xtype = 'F' and object_name(parent_obj) like 'temptb_%'
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' and name like 'temptb_%'
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



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/htl258/archive/2009/07/16/4352355.aspx
水族杰纶 2010-01-08
  • 打赏
  • 举报
回复
declare @s varchar(1000)
set @s=''
select @s=@s+' drop table '+name+';' from sysobjects where type='U' and name like'temptb%'
exec( @s)
dawugui 2010-01-08
  • 打赏
  • 举报
回复
不知道以下语句在2008中是否适用?
declare @sql varchar(8000)
set @sql=''
select @sql=@sql+' drop table ['+name+'] ' from sysobjects where xtype='u' and name like 'temptb_%'
exec(@sql)

--drop table
exec sp_msforeachtable 'if ''?'' like ''/[dbo/]./[temptb_%'' escape ''/'' drop table ? '
--delete
exec sp_msforeachtable 'if ''?'' like ''/[dbo/]./[temptb_%'' escape ''/'' delete ? '
lovezx1028 2010-01-08
  • 打赏
  • 举报
回复
ding .

34,876

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧