Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 return codes for Backups...

Author  Topic 

Wanderer
Master Smack Fu Yak Hacker

1168 Posts

Posted - 2004-12-20 : 10:58:10
Anyone know how I can get return codes on the success of a backup, so my SP can gracefully respond regards the success of a backup?

The basic code below was working, before I tried adding the @ReturnCode section - I have tried various variations.

if exists (select 'SP exists' from sysobjects where name = 'up_autobackup_dbs' and xtype = 'p')
drop proc up_autobackup_dbs
go

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

create procedure up_autobackup_dbs
@dbname varchar(255),
@backuppath varchar(255)


-- Created By: Regan Galbraith
-- Created on: 2004-07-20
--
-- Purpose: This stored procedure will create a "scrubbed" database backup
--
-- It creates backups using a specific naming standard for the backup, namely DBNAME_scrubbed.bak
-- This is used as part of a process to automate restoring and scrubbing of database backups.
--
-- Example:
-- Usage: exec up_autobackup_dbs 'Model','D:\'
--
-- Possible future additions:
-- 1> Currently, the usage is for 1 call, and and external process (cursor) is needed to run the SP
-- providing the required database names. A version could be made that runs for all DBs
--
-- Change Control: v1.0 - original version 2004-07-20
-- v1.1 - change 2004-07-26 - Regan Galbraith
-- added [ ] to cater for dbs named with numerics or reserved words
--


as

DECLARE @ReturnCode int
Set @ReturnCode = 0

begin
set nocount on
set quoted_identifier off
declare @backupSQL varchar(512)
set @backupSQL = '@returncode=backup database ['+@dbname+'] to disk = '''+@backuppath+@dbname+'_scrubbed.bak'' with init'

--print @backupSQL
exec @backupSQL
--also tried exec @returncode = @backupSQL --naturally, didn't have the @returnCode in the @BackupSQL then.
--Also tried exec @returncode=exec @backupSQL

If @ReturnCode <> 0
Begin
Print 'Error backing up DBs'
set @ReturnCode = 1
end
RETURN @ReturnCode

END



GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

declare @Rc int

set @rc=9
exec @Rc=up_autobackup_dbs 'Model','c:\'
print @rc


The only other option I can see is to do the backup, and then query the MSDB tables to determine if there is a backup row, and if not, then return 1, but that feels ugly, whereas a simple return code from the backup command feels like the way it should be.

Thoughts?

*##* *##* *##* *##*

Chaos, Disorder and Panic ... my work is done here!

Kristen
Test

22859 Posts

Posted - 2004-12-20 : 12:47:37
Does:

BACKUP DATABASE ...
SELECT @returncode = @@ERROR

provide anything useful?

Kristen
Go to Top of Page

Wanderer
Master Smack Fu Yak Hacker

1168 Posts

Posted - 2004-12-21 : 02:40:03
I thought I had tried that, but let me try again.

Any idea how to make the backup fail, so I can test it? Not that I would normal want a backup to fail. :-)

I guess I could backup to a device that isn't big enough, or an invalid disk address.

*##* *##* *##* *##*

Chaos, Disorder and Panic ... my work is done here!
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2004-12-21 : 12:42:06
I reckon backing up to a non-existent folder would be a good place to start.

Disk full would be a good one too, but better testing that on a Test server, rather than a production one!

Kristen
Go to Top of Page

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2004-12-21 : 14:15:35
FWIW, I got an error using a ficticious DB name...

HTH

=================================================================

The best things in life are nearest: Breath in your nostrils, light in your eyes, flowers at your feet, duties at your hand, the path of right just before you. -Robert Louis Stevenson, novelist, essayist, and poet
(1850-1894)
Go to Top of Page

Wanderer
Master Smack Fu Yak Hacker

1168 Posts

Posted - 2004-12-22 : 05:27:42
Thanks Guys!

Kristen - sometimes the simple things, eh? I somehow decided they (MS) wouldn't use @@error, and must never have texsted it. Seem to work a charm.

Ta

*##* *##* *##* *##*

Chaos, Disorder and Panic ... my work is done here!
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2004-12-22 : 07:25:36
Is this a good moment to admit that I was guessing?

<fx:Toddles off to add Error Checking code to Backup routine ...>

Kristen
Go to Top of Page
   

- Advertisement -