SQL清空表

来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 06:39 点击:

清空表中数据
delete from tablename
truncate table tablename
利用游标清理所有表
declare @trun_name varchar(50)
declare mycursor cursor
for select 'truncate table '+ name from sysobjects where xtype='U' and status>0
open mycursor
fetch next from mycursor into @trun_name
while @@FETCH_STATUS=0
being
exec(@trun_name)
print 'truncated table '+@trun_name
 
fetch next from mycursor into @trun_name
end
close mycursor
deallocate mycursor
 
 
1. Delete all tables
DECLARE @tablename varchar(50)
DECLARE @truncatesql varchar(255)
DECLARE TrCun_Cursor CURSOR FOR
select [name] from sysobjects where type = 'U'
-- or select [name] from sysobjects where type = 'U' and name 'table_you_do_NOT_want_to_delete'
OPEN TrCun_Cursor
FETCH TrCun_Cursor INTO
@tablename
WHILE(@@fetch_status = 0)
BEGIN
SET @truncatesql = 'truncate table ' + @tablename
--exec(@truncatesql)
PRINT @truncatesql
FETCH TrCun_Cursor INTO @tablename
END
CLOSE TrCun_Cursor
DEALLOCATE TrCun_Cursor
 
2. Truncate
EXEC sp_MSforeachtable "truncate table ?"
3. Delete and Truncate tables started with YDS
sp_MSforeachtable @command1 = "delete from ?",@whereand="and name like 'YDS_%'"
sp_MSforeachtable @command1 = "truncate table ?",@whereand="and name like 'YDS_%'"


作者“探索者”

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • sql常见面试题
      • SQL SERVER 2005性能之跟踪
      • SQL编程(一)
      • LINUX上RMAN自动备份脚本
      • sql server面试题
      • 如何将多个SQL查询统计结果一次显示出来
      • 浅谈SQL Server中的事务日志(三)----在简单恢复模式下日志的角色
      • SQL小技巧系列 --- 行转列合并
      • sql server 列转行
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1