Sunday, March 1, 2009

Backup Script Cursor Skips Databases - using Jobs and Maintanence

We had a custom made script for taking backup of SQL databases. Netbackup will call our script as pre-backup script and once script completes taking backup, then Netbackup will move that to tape.

We have a prolonging issue of this custom made script skipping all user databases and taking backup of only master, model and msdb.

Below is part of our Script:
Declare cur cursor for Select name from master.dbo.sysdatabases with (nolock) Where not name in ('tempdb')

open cur
Fetch next from cur into @DBName

We had no clue of why this is happening... Just thought that CURSOR is the culprit because this cursor need to hold the result for more time because each datbase backup will take its own sweet time.

So we introduced INSENSITIVE keyword to BACKUP command so it became

Declare cur INSENSITIVE cursor for Select name from master.dbo.sysdatabases with (nolock) Where not name in ('tempdb')

open cur
Fetch next from cur into @DBName

and now the skipping datbase issue is completely resolved after monitoring for around 1 month.