SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Trigger Error event notification IF (@@ERROR > 50000


Trigger Error event notification IF (@@ERROR > 50000

Author
Message
Peter D-251663
Peter D-251663
SSC-Enthusiastic
SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)

Group: General Forum Members
Points: 188 Visits: 223
I have a refresh script that I would like to trigger an email if an error condition greater that 50,000 is reached. This will help me determine if the script failed from insufficient disk space or exclusive access could not be given. We use litespeed and the current script will return an error on either condition greater than 50000.

Here is part of the restore script (more gets executed after, but I'd like to get the error notification to work most importantly on this restore section):
---Script--------------------
raiserror('Step 3. Restore Database to Source SQL Server via lite speed',1,1) with nowait
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
IF (@@ERROR > 50000 OR @ReturnCode > 50000) GOTO QuitWithRollback
ELSE
PRINT 'NO ERRORS in script'
--IF (@@ERROR = 62309) GOTO QuitWithRollback
exec master.dbo.xp_restore_database
@database = 'database123',
@filename = '\\server1\backupdatabase123.BAK',
@filenumber = 1,
@with = 'REPLACE',
@with = 'RECOVERY',
@with = 'NOUNLOAD',
@with = 'STATS = 10',
@with = 'MOVE N''EP88SYS_Data'' TO N''E:\MSSQL\Data\database123.MDF''',
@with = 'MOVE N''EP88SYS_Log'' TO N''E:\MSSQL\database123_Log.LDF''',
@with = 'STATS = 10'


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 = 'admin@mail.ca',
@Subject = 'Refresh failed due to Exclusive Access could not be obtained OR There is insufficient free space',
@Body = 'Check Refresh logs asRefresh failed due to Exclusive Access could not be obtained OR There is insufficient free space'
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION

--When I run the above the QuitWithRollback section always executes regardless of an error or not and need help getting the right things in the right places so that it only 'QuitWithRollback:' executes when the following occur:

Step 3. Restore Database to Source SQL Server via lite speed
Msg 62309, Level 19, State 1, Line 1
SQL Server has returned a failure message to LiteSpeed 2005 which has
prevented the operation from succeeding.
The following message is not a LiteSpeed 2005 message. Please refer to
SQL Server books online or Microsoft technical support for a
solution

Appreciate help
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search