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 上数据库清空 ,需要把注释的语句依次执行。

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

  • 相关阅读:
    maven资源文件的相关配置
    servlet-url-pattern匹配规则详细描述
    Spring的单例模式底层实现
    jsf--小项目--爱群小店
    jsf--页面循环跳转,项目内容递交
    查看MySQL路径
    HTML和XHTML的区别是什么
    Jsf 页面导航Navigation总结
    h:commandButton
    JSF--INTRODUCION
  • 原文地址:https://www.cnblogs.com/zhutao1015/p/3272763.html
Copyright © 2011-2022 走看看