Dzwebs.Net

撰写电脑技术杂文十余年

一步就能清理删除SQL所有数据表的三种方法

Admin | 2014-11-20 9:26:38 | 被阅次数 | 9727

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

  如果SQL数据表非常多,一张一张的清空,实在麻烦,因此,我们可以借助SQL语句来删除这些数据表。使用的数据库为MS SQL SERVER。

  第一种方法:搜索出所有表名,构造为一条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)

  缺点:该方法适合表不是非常多的情况,否则表数量过多,超过字符串的长度,不能进行完全清理。

  第二种方法:利用游标删除所有表

  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

  上述语句可以做为存储过程调用, 能够一次清空所有表的数据,并且还可以进行有选择的清空表。

  第三种方法:利用微软未公开的存储过程

  exec sp_msforeachtable "truncate table ?"

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


该杂文来自: 数据库Sql,VFP,Access

上一篇:mssql2005在哪创建关系

下一篇:如何让Access一次性执行多条SQL语句

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计