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.
| 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_dbsgoSET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS ON GOcreate 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--asDECLARE @ReturnCode intSet @ReturnCode = 0beginset nocount onset quoted_identifier offdeclare @backupSQL varchar(512)set @backupSQL = '@returncode=backup database ['+@dbname+'] to disk = '''+@backuppath+@dbname+'_scrubbed.bak'' with init'--print @backupSQLexec @backupSQL--also tried exec @returncode = @backupSQL --naturally, didn't have the @returnCode in the @BackupSQL then.--Also tried exec @returncode=exec @backupSQLIf @ReturnCode <> 0 Begin Print 'Error backing up DBs' set @ReturnCode = 1endRETURN @ReturnCodeENDGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOdeclare @Rc intset @rc=9exec @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 = @@ERRORprovide anything useful?Kristen |
 |
|
|
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! |
 |
|
|
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 |
 |
|
|
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) |
 |
|
|
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! |
 |
|
|
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 |
 |
|
|
|
|
|
|
|