User Tools

Site Tools


computing:windows:mssql_backup

This is an old revision of the document!


Microsoft SQL Server Backup

Database Recovery Models

Script

Set all databases to the FULL recovery model. Replace the three occurrences of “full” with “simple” if desired.

USE master
GO

-- Declare a variable to store the value [database name] returned by FETCH.
DECLARE @dbname sysname, @cmd varchar(1000)

-- Declare a cursor to iterate through the list of databases

DECLARE db_recovery_cursor CURSOR FOR
SELECT name from sysdatabases

-- Open the cursor
OPEN db_recovery_cursor

-- Perform the first fetch and store the value in a variable.
FETCH NEXT FROM db_recovery_cursor INTO @dbname

-- loop through cursor until no more records fetched
WHILE @@FETCH_STATUS = 0
BEGIN
IF (SELECT DATABASEPROPERTYEX(@dbname,'RECOVERY')) <> '<RECOVERYMODEL>' and @dbName <> 'tempdb' BEGIN

-- create the alter database command for each database
SET @cmd = 'ALTER DATABASE "' + @dbname + '" SET RECOVERY <RECOVERYMODEL>'

-- alter each dataabase setting the recovery model to <RECOVERYMODEL>

EXEC(@cmd)
PRINT @dbname
END

FETCH NEXT FROM db_recovery_cursor INTO @dbname
END

-- close the cursor and deallocate memory used by cursor
CLOSE db_recovery_cursor
DEALLOCATE db_recovery_cursor
computing/windows/mssql_backup.1327122930.txt.gz · Last modified: 2012/01/20 22:15 by gcooper