SQL Server implementation automatically shrinks database log files

Source: Internet
Author: User

1 --the following SQL code is executed in the job to enable automatic shrinking of the log files. (The ability to automatically shrink log files is not available in the MSSQL maintenance plan)2 --non-mirrored replication subscriptions are executed in the case of log shipping. Log shipping fails if log shipping does not exist3 Declare @exec_sql nvarchar( -),@simple_sql nvarchar( -),@full_sql nvarchar( -)4 Declare @db_name varchar( -)5 Declare @db_count int6 Select @db_count=COUNT(1) fromsys.databaseswheredatabase_id>47 Declare @j int8 Set @j =19 Ten  while @j<=@db_count  One begin A         Select @db_name=Name from(SelectRow_number () Over(Order  byName) Rn,name fromsys.databaseswheredatabase_id>4) asTmpwhereRn=@j -         SET @simple_sql=N'Use [master]' + Char(Ten)+ Char( -)+'Alter DATABASE ['+@db_name+'] SET RECOVERY simple with no_wait' -         EXECsp_executesql@simple_sqlN'@db_name varchar (+)',@db_name the          -         SET @exec_sql=N'Use ['+@db_name+']'+ Char(Ten)+ Char( -)+'DBCC shrinkfile (N" "+@db_name+'_log"', 0, truncateonly)' -         EXECsp_executesql@exec_sqlN'@db_name varchar (+)',@db_name -          +         SET @full_sql =N'Use [master]' + Char(Ten)+ Char( -)+'Alter DATABASE ['+@db_name+'] SET RECOVERY full with no_wait' -         EXECsp_executesql@full_sqlN'@db_name varchar (+)',@db_name +                  A     Set @j =@j+1 at End

SQL Server implementation automatically shrinks database log files

Related Article

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.