Friday, May 7, 2010

Database Backup by Stored Procedure

---------------
--Backup all db
---------------
DECLARE @name VARCHAR(50)
DECLARE @path VARCHAR(256)
DECLARE @fileName VARCHAR(256)
DECLARE @fileDate VARCHAR(20)

SET @path = 'C:\backup\DB\' ---Give Your Path Here where you want to store this backup file
SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),105) -- Add Date in File Name
DECLARE db_cursor CURSOR FOR
SELECT name
FROM master.dbo.sysdatabases
where name = 'Kool_Project_Test' ---Name of Database that you want to get Backup

OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name

WHILE @@FETCH_STATUS = 0
BEGIN
SET @fileName = @path + @name +'_'+ @fileDate + '.BAK'
BACKUP DATABASE @name TO DISK = @fileName WITH NOFORMAT, INIT

FETCH NEXT FROM db_cursor INTO @name
END

CLOSE db_cursor
DEALLOCATE db_cursor

No comments:

Post a Comment