zoukankan      html  css  js  c++  java
  • sql 清空所有数据

    近来发现数据库过大,空间不足,因此打算将数据库的数据进行全面的清理,但表非常多,一张一张的清空,实在麻烦,因此就想利用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)

    该方法适合表不是非常多的情况,否则表数量过多,超过字符串的长度,不能进行完全清理.
    2.利用游标清理所有表
    go
    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 ?"

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

    记得要删除外键。。。

    转载 请注明原文地址并标明转载:http://www.cnblogs.com/laopo 商业用途请与我联系:lcfhn168@163.com
  • 相关阅读:
    第九章、查找
    opencv- python使用
    opencv初入
    初入
    第四章、数学问题
    数据结构
    分享一个SQLSERVER脚本
    详解SQL语句的集合运算
    数据库权限分配探讨
    数据库分区分表以及读写分离
  • 原文地址:https://www.cnblogs.com/laopo/p/4191300.html
Copyright © 2011-2022 走看看