qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請?jiān)L問 http://qaseven.github.io/

          好用的清理數(shù)據(jù)庫腳本

          1、腳本說明

            此腳本用于清空數(shù)據(jù)庫數(shù)據(jù),只刪除相關(guān)表記錄,保留表結(jié)構(gòu)及存儲過程觸發(fā)器等主要架構(gòu)。

            設(shè)計(jì)思路:

            1)根據(jù)表添加時間逆向獲取所有用戶表信息

            2)使用游標(biāo)循環(huán)刪除每張表內(nèi)數(shù)據(jù)

            3)使用delete進(jìn)行刪除,即使有外鍵關(guān)系同樣可以刪除表記錄

            4)表存在自增主鍵則將其重置為0

            5)截?cái)?a target="_self" style="word-break: break-all; color: #202859; text-decoration: none; line-height: normal !important; ">日志,將數(shù)據(jù)庫表空間及日志文件縮減到最小

            2、使用說明

            1)建立刪除數(shù)據(jù)庫存儲過程SP_DaTaBaSeClear

          以下是代碼片段:
          If( object_id('SP_DaTaBaSeClear') is not null ) drop procedure SP_DaTaBaSeClear go SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE PROCEDURE SP_DaTaBaSeClearASBegin Transaction declare @BtableName varchar(200) declare curDel cursor for select rtrim(name) from sysobjects where type = 'U' order by crdate desc open curDel declare @delSQL varchar(500) fetch next from curDel into @BtableName while( @@fetch_status = 0) begin set @delSQL = 'delete from ' + @BtableName print @delSQL exec( @delSQL ) if( ident_seed(@BtableName) is not null ) begin dbcc checkident( @BtableName, reseed, 0 ) print '種子成功置為1' end fetch next from curDel into @BtableName end close curDel deallocate curDel Commit GO

            2)執(zhí)行該存儲過程,執(zhí)行過程中查看執(zhí)行信息,如有紅色信息則先手動刪除紅色信息表記錄

            -- 執(zhí)行存儲過程刪除表數(shù)據(jù)

            EXEC SP_DaTaBaSeClear

            3)如仍然報(bào)出紅色信息則直接執(zhí)行以下語句進(jìn)行刪除

          以下是代碼片段:
          declare @BtableName varchar(128) declare curDel cursor for select rtrim(name) from sysobjects where type = 'U' order by crdate desc open curDel declare @delSQL varchar(255) fetch next from curDel into @BtableName while( @@fetch_status = 0) begin set @delSQL = 'delete from ' + @BtableName print @delSQL exec( @delSQL ) if( ident_seed(@BtableName) is not null ) begin dbcc checkident( @BtableName, reseed, 0 ) print '種子成功置為1' end fetch next from curDel into @BtableName end close curDel deallocate curDel

            4)最后執(zhí)行腳本重置數(shù)據(jù)庫大小

          以下是代碼片段:
              backup log @DataBaseName with no_log dbccshrinkdatabase(@DataBaseName)
            dbccupdateusage(@DataBaseName)

          posted on 2011-12-26 11:40 順其自然EVO 閱讀(243) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          <2011年12月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 汉阴县| 大余县| 海安县| 石景山区| 尚义县| 庆城县| 梨树县| 香港| 锦屏县| 扎鲁特旗| 咸宁市| 贵阳市| 子洲县| 彰化市| 龙岩市| 禹州市| 福州市| 平湖市| 句容市| 芮城县| 海兴县| 新营市| 正安县| 含山县| 军事| 舞钢市| 大同县| 班戈县| 洪泽县| 密云县| 乐安县| 佛教| 安化县| 沙雅县| 六安市| 德州市| 宁陕县| 新闻| 东平县| 阳西县| 中西区|