——————————-迴圈資料庫壓縮記錄檔——R

來源:互聯網
上載者:User
--迴圈資料庫壓縮記錄檔(假設記錄檔fileid=2)
create table #db(id int identity(1,1),name varchar(80),dbsize int,mdfsize int,ldfsize int)
insert into #db
 select a.name,SUM(b.size)*8/1024 as 'dbsize[MB]',
  SUM(case when b.type<>1 then b.size else 0 end)*8/1024 as 'datafilesize[MB]',
  SUM(case when b.type=1 then b.size else 0 end)*8/1024 as 'logfilesize[MB]'
 from sys.databases as a
  inner join sys.master_files as b on a.database_id=b.database_id
 where a.database_id>4
 group by a.name
 order by SUM(case when b.type=1 then b.size else 0 end)--按日誌從大到小排序
declare @min_id int,@max_id int,@tmp_db varchar(80),@s varchar(max)
select @min_id=min(id),@max_id=max(id) from #db where ldfsize>0--此處指定記錄檔大於多少M的
while @min_id<=@max_id
 begin
  select @tmp_db=name,@min_id=@min_id+1 from #db where id=@min_id
  set @s='BACKUP LOG '+@tmp_db+' with no_log'
  exec(@s)
  set @s='use '+@tmp_db+';dbcc shrinkfile(2)'
  exec(@s)
 end
 drop table #db

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.