Join our community of customers. Call us at
1-888-313-9421 to discuss your needs or request a quote, or email us at: sales@orcsweb.com

SQL Server 2008: Truncate Log While Maintaining the Database Recovery Model

by Desiree 31. October 2008 10:51

Hi,

 

Here is a script that will truncate the database log while maintaining the database recovery model. 

 

--Notes: Be sure to update the variable @sDbName to the
--correct database name.

 

DECLARE @sDbName VarChar(40)
SET @sDbName = 'DB_Name' --Add database name here

 

DECLARE @sDbRecovery varchar(40)
DECLARE @sLogName varchar(40)
DECLARE @sDBIsSimple_Shrink varchar(4000)
DECLARE @sSetRecoverySimple varchar(4000)
DECLARE @sShrinkDBLog varchar(4000)
DECLARE @sReSetRecovery varchar(4000)
DECLARE @sGetLogName varchar(4000)

 

-- the @dbrecovery variable is set to the recovery
-- model of the database

Set @sDbRecovery = CAST(DATABASEPROPERTYEX(@sDbName, 'Recovery') AS varchar(40))

 

-- this variable is used to get the logical log file
-- name of the database
set @sGetLogName =('USE ' + @sDbName + '
                  select name from sys.database_files where type = 1
                  ')

 

-- a temporary table is created to hold the logical
-- file name of the database and then sets the @LogName
-- variable to that value. The table is then dropped.

-- Drop the table if it exists
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ResultSet]') AND type in (N'U'))
DROP TABLE [dbo].[ResultSet]

 

-- create table and set the @LogName varable
CREATE TABLE ResultSet (SetLogName VarChar(400))
INSERT INTO ResultSet EXEC(@sGetLogName)
SET @sLogName = (select SetLogName from ResultSet)
Drop Table ResultSet

 

-- the next set of statements truncate the log,
-- but also ensures the recovery model of the
-- database is maintained.
IF @sDbRecovery = 'Simple'
      BEGIN
            set @sDBIsSimple_Shrink ='USE ' + @sDbName + '
            DBCC SHRINKFILE (' + @sLogName + ', 1)
            '
            EXEC(@sDBIsSimple_Shrink)
      END
ELSE IF @sDbRecovery = 'BULK_LOGGED'
      BEGIN       

-- Truncate the log by changing the
-- database recovery model to SIMPLE.
            set @SsetRecoverySimple = 'ALTER DATABASE ' + @sDbName + '
          SET RECOVERY SIMPLE'
            EXEC (@sSetRecoverySimple)
            -- Shrink the truncated log file to 1 MB.
            set @sShrinkDBLog ='USE ' + @sDbName + '
            DBCC SHRINKFILE (' + @sLogName + ', 1)
            '
            EXEC(@sShrinkDBLog)
            -- Reset the database recovery model.
            set @sReSetRecovery = 'ALTER DATABASE ' + @sDbName + '
          SET RECOVERY BULK_LOGGED'
            EXEC (@sReSetRecovery)
      END
ElSE
      BEGIN

-- Truncate the log by changing the database
-- recovery model to SIMPLE.
            set @sSetRecoverySimple = 'ALTER DATABASE ' + @sDbName + '
            SET RECOVERY SIMPLE'
            EXEC (@sSetRecoverySimple)
            -- Shrink the truncated log file to 1 MB.
            set @sShrinkDBLog ='USE ' + @sDbName + '
            DBCC SHRINKFILE (' + @sLogName + ', 1)
            '
            EXEC(@sShrinkDBLog)           
            -- Reset the database recovery model.
            set @sReSetRecovery = 'ALTER DATABASE ' + @sDbName + '
          SET RECOVERY FULL'
            EXEC (@sReSetRecovery)
      END

 

Comments are closed
Copyright ©1996-2008 ORCS Web, Inc. All rights reserved.
Powered by BlogEngine.NET 1.4.5.0. Log in.