Search

Thursday, July 28, 2011

Backup cursor for taking backup of all databases in a server

This cursor lists all databases ( exept system database, if you want them, remove the where clause) and backups them to the given location. Can be used for MSDE etc. where you have no maint plans or tools for backup. Also valid for dbcc commands etc.

declare Backup_cursor cursor
for
select [name] from sysdatabases where dbid >4 order by dbid
open Backup_cursor
declare @dbname sysname
DECLARE @SQLString NVARCHAR(500)
fetch next from Backup_cursor into @dbname
while (@@fetch_status <> -1)
begin
SET @SQLString = N'backup database ' + @dbname + ' to disk =''f:\' + @dbname +'.bak'' with differential,init '
EXEC(@SQLString)
fetch next from Backup_cursor into @dbname
end
close Backup_cursor
deallocate Backup_cursor

No comments:

Post a Comment