Skip to content
Category: SQL Server
2012-03-30

Script: Reduce Log File depending on DB Data Files size

A friend asked me about a dynamic script to reduce the DB Log File based on the total size of DB Data Files, here is the answer:

/*--------------------------------------------------------------------------------------
-- File: ShrinkToPercent.sql
-- Author: Fran Lens (http:\\www.lensql.net)
-- Date: 2012-03-27
-- Description: Reduce LogFile Size based on percentage of the Total Size of DataFiles
--------------------------------------------------------------------------------------*/
DECLARE @SelectDB varchar(50)
DECLARE @ShrinkPercent float
DECLARE @DBid int
DECLARE @RecoverySimple varchar(200)
DECLARE @RecoveryFull varchar(200)
DECLARE @ShrinkCommand nvarchar(200)
DECLARE @ShrinkFile varchar(50)
DECLARE @ShrinkValue varchar(50)

SET @SelectDB = 'AdventureWorks2008R2' -- Database whose Log will be Reduced
SET @ShrinkPercent = 30 -- Percentage of the DataFiles Size to Reduce the LogFile
-- Example: With a value of 20 Percent and 1GB of Datafiles Size, the Log will be reduced to 200MB

SET @DbId= (select database_id from sys.databases where name=@SelectDB)
SET @RecoverySimple = 'ALTER DATABASE [' + @SelectDB + '] SET RECOVERY SIMPLE WITH NO_WAIT' -- Change the recovery model to Simple
SET @ShrinkFile = (SELECT name from sys.master_files WHERE database_id = @DBid and type_desc = 'LOG')
SET @ShrinkValue = (@ShrinkPercent)/100 * (SELECT SUM(size)/128 FROM sys.master_files WHERE database_id = @DBid and type_desc = 'ROWS')
SET @ShrinkCommand = 'USE [' + @SelectDB + ']' + CHAR(13)+ 'DBCC SHRINKFILE('+ @ShrinkFile+',' + @ShrinkValue + ')' -- Reduce the LogFile Size
SET @RecoveryFull = 'ALTER DATABASE [' + @SelectDB + '] SET RECOVERY FULL WITH NO_WAIT' -- Change the recovery model to Full

EXEC (@RecoverySimple)
EXEC sp_executesql @ShrinkCommand
EXEC (@RecoveryFull)

Complete este formulario para recibir la guía de Windows Server en Azure
*Obligatorio
Complete este formulario para recibir la guía de Windows Server en Azure
Gracias por rellenar el formulario [name]. ¡Aquí tienes tu eBook Gratis!
Complete este formulario para recibir 4 best practices to implement a comprehensive Zero Trust security approach
*Obligatorio
Complete este formulario para recibir 4 best practices to implement a comprehensive Zero Trust security approach
Gracias por rellenar el formulario [name]. ¡Aquí tienes tu eBook Gratis!
Complete este formulario para recibir Cloud Migration Essentials
*Obligatorio
Complete este formulario para recibir Cloud Migration Essentials
Gracias por rellenar el formulario [name]. ¡Aquí tienes tu eBook Gratis!
Complete este formulario para recibir Cloud security Advice for Nonprofit Leaders
*Obligatorio
Complete este formulario para recibir Cloud security Advice for Nonprofit Leaders
Gracias por rellenar el formulario [name]. ¡Aquí tienes tu eBook Gratis!
Complete este formulario para recibir Prevent data leaks with Microsoft 365 Business Premium
*Obligatorio
Complete este formulario para recibir Prevent data leaks with Microsoft 365 Business Premium
Gracias por rellenar el formulario [name]. ¡Aquí tienes tu eBook Gratis!
Complete this form to recieve the guide of Windows Server on Azure
*Required
Complete this form to recieve the guide of Windows Server on Azure
Thank you for filling out the form [name]. Here's your Free eBook!
Complete this form to recieve 4 best practices to implement a comprehensive Zero Trust security approach
*Required
Complete this form to recieve 4 best practices to implement a comprehensive Zero Trust security approach
Thank you for filling out the form [name]. Here's your Free eBook!
Complete this form to recieve Cloud Migration Essentials
*Required
Complete this form to recieve Cloud Migration Essentials
Thank you for filling out the form [name]. Here's your Free eBook!
Complete this form to recieve Cloud security Advice for Nonprofit Leaders
*Required
Complete este formulario para recibir Cloud security Advice for Nonprofit Leaders
Thank you for filling out the form [name]. Here's your Free eBook!
Complete this form to recieve Prevent data leaks with Microsoft 365 Business Premium
*Obligatorio
Complete this form to recieve Prevent data leaks with Microsoft 365 Business Premium
Thank you for filling out the form [name]. Here's your Free eBook!
Complete this form to recieve Cloud Migration Simplified Ebook.
*Obligatorio
Complete this form to recieve Prevent data leaks with Microsoft 365 Business Premium
Thank you for filling out the form [name]. Here's your Free eBook!