The instance has eight databases and only backs up the first four and the jobs ends with no errors. it just does half of the databases in the cursor.
SET NOCOUNT ON
go
SET QUOTED_IDENTIFIER OFF
go
USE master
go
declare db_list_csr cursor for
select name
from sysdatabases
where name not in ('tempdb','Northwind','pubs')
order by name
go
declare @had_error bit
declare @currtime char(19)
declare @dbname varchar(45)
declare @file0 varchar(45), @file1 varchar(45), @file2 varchar(45)
declare @msg varchar(255)
/* Initialize stuff */
select @had_error = 0
open db_list_csr
fetch next from db_list_csr into @dbname
while @@fetch_status=0
begin
select @file0="g:\DBA\Backups\"+@dbname+".bak"
select @currtime = getdate()
select @msg = "Start dumping database "+@dbname+" at "+@currtime
print @msg
backup database @dbname to disk= @file0
if @@error = 0
begin
select @msg = "Database dump of "+@dbname+" completed at "+@currtime
print @msg
end
else
begin
select @had_error = 1
print "**********************************************************"
print "**********************************************************"
select @msg = "Database dump of "+@dbname+" failed at "+@currtime
print @msg
print "**********************************************************"
print "**********************************************************"
end
fetch next from db_list_csr into @dbname
end
close db_list_csr
deallocate db_list_csr
if @had_error = 1
goto error
else
print "That's all!"
return
error:
print "*******************************"
print "* DATABASE DUMPS HAD ERRORS *"
print "*******************************"
return
go