Below cursor execution will backup all your databases except in system databases in to a specified location on your hard disk.
DECLARE @name VARCHAR(50) -- database name
DECLARE @path VARCHAR(256) -- path for backup files
DECLARE @fileName VARCHAR(256) -- filename for backup
DECLARE @fileDate VARCHAR(20) -- used for file name
SET @path = 'C:\Backup\' -- set the backup path
SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)
DECLARE db_cursor CURSOR FOR -- declare cursor
SELECT name
FROM MASTER.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb')
OPEN db_cursor -- open cursor
FETCH NEXT FROM db_cursor INTO @name -- fetching records from using cursor
WHILE @@FETCH_STATUS = 0 -- while end of all records
BEGIN
SET @fileName = @path + @name + '_' + @fileDate + '.BAK'
BACKUP DATABASE @name TO DISK = @fileName
FETCH NEXT FROM db_cursor INTO @name -- continue loop
END
CLOSE db_cursor -- close cursor
DEALLOCATE db_cursor
Original Source - http://tranphuonghoang.wordpress.com/2012/10/16/sql-server-cursor-example/
Saturday, November 24, 2012
Backup Sql Server databases using cursor
Subscribe to:
Post Comments (Atom)
No comments:
Write comments