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
 Transact-SQL (2000)
 Error Handling help needed!

Author  Topic 

netwerkassist
Starting Member

13 Posts

Posted - 2011-09-21 : 20:55:59
I have database refresh script, and want it to email me and halt the script when an error occurs on a specific step as shown below (network path not correct):

raiserror('Step 1. Copy Production Backup file over to Sustainment SQL Server',1,1) with nowait

DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

EXEC master..xp_cmdshell 'COPY \\server1\backup$\HR83PRD_Full_LiteSpeed.BAK \\server2\backup$\HR83PRD_Full_LiteSpeed.BAK'

QuitWithRollback:
declare @dbname varchar(500)
declare @dest varchar(1000)
declare @path varchar(512)
declare @stt varchar (5000)
declare @subject_str varchar(1000)
declare @from_str varchar(500)
declare @now datetime
EXEC master..usp_sendcdomail

@From = 'server1@mail.ca',
@To = 'user1@mail.ca',
@Subject = 'The network path was not found',
@Body = 'Check and validate Network path for Production backupfile'
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
RETURN


--However even if the network path is correct it still triggers the email and halts the script.

Appreciate input as the best way to handle this.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-21 : 22:42:10
Why are checking the value of @@ERROR before you do anything that would cause an error?

Wrap this into a stored procedure, and you'll be able to use RETURN to exit instead of dropping into the GOTO section. It'll drop in there otherwise.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

netwerkassist
Starting Member

13 Posts

Posted - 2011-09-23 : 11:07:45
Thanks Tara for our help, I'll see if I can rework it based on your suggestions.

Peter






quote:
Originally posted by tkizer

Why are checking the value of @@ERROR before you do anything that would cause an error?

Wrap this into a stored procedure, and you'll be able to use RETURN to exit instead of dropping into the GOTO section. It'll drop in there otherwise.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

Go to Top of Page
   

- Advertisement -