使用SQL语句清空数据库所有表的数据
来源:高中英语教学交流网
发布时间:2013-11-08 14:51:00
查看次数:
内容提要:3.利用微软未公开的存储过程
近来发现数据库过大,空间不足,因此打算将数据库的数据进行全面的清理,但表非常多,一张一张的清空,实在麻烦,因此就想利用SQL语句一次清空所有数据.找到了三种方法进行清空.使用的数据库为MS SQL SERVER.
1.搜索出所有表名,构造为一条SQL语句
declare @trun_name varchar(8000)
set @trun_name=''
select @trun_name=@trun_name + 'truncate table ' + [name] + ' ' from sysobjects where xtype='U' and status > 0
exec (@trun_name)
set @trun_name=''
select @trun_name=@trun_name + 'truncate table ' + [name] + ' ' from sysobjects where xtype='U' and status > 0
exec (@trun_name)
该方法适合表不是非常多的情况,否则表数量过多,超过字符串的长度,不能进行完全清理.
2.利用游标清理所有表
declare @trun_name varchar(50)
declare name_cursor cursor for
select 'truncate table ' + name from sysobjects where xtype='U' and status > 0
open name_cursor
fetch next from name_cursor into @trun_name
while @@FETCH_STATUS = 0
begin
exec (@trun_name)
print 'truncated table ' + @trun_name
fetch next from name_cursor into @trun_name
end
close name_cursor
deallocate name_cursor
declare name_cursor cursor for
select 'truncate table ' + name from sysobjects where xtype='U' and status > 0
open name_cursor
fetch next from name_cursor into @trun_name
while @@FETCH_STATUS = 0
begin
exec (@trun_name)
print 'truncated table ' + @trun_name
fetch next from name_cursor into @trun_name
end
close name_cursor
deallocate name_cursor
这是我自己构造的,可以做为存储过程调用, 能够一次清空所有表的数据,并且还可以进行有选择的清空表.
3.利用微软未公开的存储过程
exec sp_msforeachtable "truncate table ?"
该方法可以一次清空所有表,但不能加过滤条件.
如果只是删除某一个表,就使用这个代码
TRUNCATE TABLE '表名'
- 相关文章
- ·超简单复制百度文库和豆丁网内容05-31·自动双面打印操作说明10-14·如何压缩word?10-19·word表格转excel表格,保持格式不变04-12·将纸质版文字材料转为电子版12-23·PS对扫描文件去除背景色04-26·Excel提取多列中唯一值07-13·轻松解决“找不到macro1$A$2”02-18·教你如何下载微信公众号的音频文件06-02·如何一次性同时打开多个Excel工作簿10-30
- 最新文章
- ·Win10系统开机启动文件夹在哪里?04-16·十秒免工具激活windows 1002-25·批量替换word文档中的第一行作为文件的文件名06-21·连接打印机时需要输入用户名密码怎么办?05-15·免魔法使用 New Bing 新方案03-18·Win10电脑账号密码设置、修改、取消12-06·[亲测有效]excel批量修改文件名10-29
- 阅读排行
- ·如何让试题的ABCD选项对齐04-13·word排版技巧整理08-02·word 如何自动生成目录08-02·不同电脑文件字体改变了的解决方法12-15·Excel 进行学生成绩统计分析03-13·我的电脑图标不见了,怎么找回来!09-14·Win10电脑的一些安装提示12-03·Word高级替换技巧03-05·Excel如何批量插入分页符02-12·制作Word2003选择性粘贴快捷键07-09
2008-2024 | www.zshunj.cn |www.yykz.net
点击这里识别二维码关注公众号
点击这里识别二维码关注公众号