清空数据

http://www.cnblogs.com/kingkoo/archive/2008/02/24/1079157.html

近来发现数据库过大,空间不足,因此打算将数据库的数据进行全面的清理,但表非常多,一张一张的清空,实在麻烦,因此就想利用SQL语句一次清空所有数据.找到了三种方法进行清空.使用的数据库为MS SQL SERVER. 1.搜索出所有表名,构造为一条SQL语句 该方法适合表不是非常多的情况,否则表数量过多,超过字符串的长度,不能进行完全清理. 2.利用游标清理所有表

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 )

 

 

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

这是我自己构造的,可以做为存储过程调用, 能够一次清空所有表的数据,并且还可以进行有选择的清空表. 3.利用微软未公开的存储过程

exec  sp_msforeachtable " truncate   table  ?"

 

该方法可以一次清空所有表,但不能加过滤条件.

删除所有表中的数据 :

sp_MSforeachtable @command1='Delete from ?'

sp_MSforeachtable @command1 = "TRUNCATE TABLE ?"

  • ?表示所有表
  • 还有一个参数为@whereend, @whereend='name in 'table1','table2',... ...'

这个参数是限制此存储过程的操作用于哪些表.

1。在sql server2000下一个数据库中有建立了几十个表,现在要给每个表增加同样的列。可以用"alter table"语句,但是这么多表一个一个的操作太麻烦,有什么方法可以操作循环每一个表呢? 方法:sp_msforeachtable 'alter table ? add 列名 int'

2。假设所有表中的'note' 字段不能为空,如果为空则要改为"。 方法:sp_msforeachtable 'update ? set note='''' where note is null'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值