----------- Compress the log file in the cyclic database -- R

Source: Internet
Author: User
-- Compress the log file in the circulating database (assuming that the log file 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) x 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
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) -- Sort logs in ascending order
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 -- specify the MB where the log file is larger
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.