zoukankan      html  css  js  c++  java
  • 清空数据库表sysobjects

    项目开发与测试过程填充大量测试数据,实施前需要清空数据库,整理一个干净的数据环境。手写delete语句很费事,所以从网上搜索相关内容整理如下

    declare @trun_name varchar(500)
     declare name_cursor cursor for
    --禁用外键
    --select  'ALTER TABLE ['  + name +  '] NOCHECK CONSTRAINT  all '  from  sysobjects  a  where  a.xtype ='u'  
    --禁用触发器
    --select  'ALTER TABLE ['  + name +  '] DISABLE  TRIGGER  all '  from  sysobjects  a  where  a.xtype ='u'  
    --清空表
    --select 'truncate table ' + name from sysobjects where xtype='U' and name not in ('xt_mksz','xt_mkczgn','xt_dwbm') order by name--排除不需要清空的表,以及清空过程报错的表需要单独处理
    --开启外键
    --select  'ALTER TABLE ['  + name +  '] CHECK CONSTRAINT  all '  from  sysobjects  a  where  a.xtype ='u'  
    --开启触发器
    --select  'ALTER TABLE ['  + name +  '] enable  TRIGGER  all '  from  sysobjects  a  where  a.xtype ='u'  
    --所有identity表复原为1
    --select  'dbcc checkident(['+name+'],reseed ,1) ' from  sysobjects  a  where  a.xtype ='u'  and objectproperty(id,'TableHasIdentity')=1  and name not in ('xt_mksz','xt_mkczgn','xt_dwbm')--排除不需要清空的表,以及清空过程报错的表需要单独处理
    --重建索引 --select 'dbcc DBREINDEX(['+name+']) ' from sysobjects a where a.xtype ='u' open name_cursor fetch next from name_cursor into @trun_name while @@FETCH_STATUS = 0 begin exec (@trun_name) print @trun_name fetch next from name_cursor into @trun_name end close name_cursor deallocate name_cursor

    以上语句,用于sql sever 上数据库清空 ,需要把注释的语句依次执行。

    本文内容来源于网络搜集,如有版权问题请联系告知

  • 相关阅读:
    Java Output流写入包装问题
    SpringBoot项目单元测试不经过过滤器问题
    SpringSecurity集成启动报 In the composition of all global method configuration, no annotation support was actually activated 异常
    JWT jti和kid属性的说明
    Maven 排除依赖
    第五章 基因概念的发现
    第三章 孟德尔遗传的拓展
    第二章 孟德尔遗传
    第一章 引言
    GWAS全基因组关联分析
  • 原文地址:https://www.cnblogs.com/zhutao1015/p/3272763.html
Copyright © 2011-2022 走看看