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 2012 Forums
 Transact-SQL (2012)
 capture tsql error message

Author  Topic 

bingobasketball
Starting Member

4 Posts

Posted - 2014-12-02 : 10:17:22
Hi
My backup failed this morning and the message in job history was:
Msg 3013, Level 16, State 1, Line 1
BACKUP DATABASE is terminating abnormally.

So I took my code and ran in SSMS:
BACKUP DATABASE [model] TO DISK = 'E:\MSSQL.1\MSSQL\BackupToNonExistingPath\model.bak'

The error message was:
Msg 3201, Level 16, State 1, Line 1
Cannot open backup device 'E:\MSSQL.1\MSSQL\BackupToNonExistingPath\model.bak'. Operating system error 3(The system cannot find the path specified.).
Msg 3013, Level 16, State 1, Line 1
BACKUP DATABASE is terminating abnormally.

So it is pretty helpful and I was able to fix the issue. Life's good, but here is the issue. I want to email myself this error, and I am particular interested in "Msg 3201" from above.
It only seems like "Msg 3013" is being logged

Question how do I capture either first of both messages.

I tried this code, see below but no luck.
BEGIN TRY

BACKUP DATABASE [model] TO DISK = 'E:\MSSQL.1\MSSQL\BackupToNonExistingPath\model.bak'

END TRY
BEGIN CATCH
DECLARE
@ErMessage NVARCHAR(2048),
@ErSeverity INT,
@ErState INT

SELECT @ErMessage = ERROR_MESSAGE(),
@ErSeverity = ERROR_SEVERITY(),
@ErState = ERROR_STATE()

SELECT @ErMessage, @ErSeverity, @ErState

RAISERROR (@ErMessage, @ErSeverity, @ErState )
END CATCH


Sorry about long post

My_Sig_Nature

Ifor
Aged Yak Warrior

700 Posts

Posted - 2014-12-02 : 10:32:25
To send yourself an email, you need to make sure Database Mail is configured, then you can use sp_send_dbmail.

http://blog.sqlauthority.com/2008/08/23/sql-server-2008-configure-database-mail-send-email-from-sql-database/

My catch blocks tend to just consist of a call to a generic exception handler:

BEGIN CATCH
EXEC dbo.ExceptionHandler;
END CATCH


with the exception handler defined like:

SET ANSI_NULLS, QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE dbo.ExceptionHandler
@ProcID int = NULL
,@ExtraMessage nvarchar(1000) = NULL
AS

SET NOCOUNT ON;

DECLARE @ErrorNumber int = ERROR_NUMBER()
,@ErrorSeverity int = ERROR_SEVERITY()
,@ErrorState int = ERROR_STATE()
,@ErrorProcedure nvarchar(126) = ERROR_PROCEDURE()
,@ErrorLine int = ERROR_LINE()
,@ErrorMessage nvarchar(2048) = ERROR_MESSAGE();

IF @@TRANCOUNT > 0
ROLLBACK;

IF @ProcID IS NOT NULL
SET @ErrorProcedure = N'[' + OBJECT_SCHEMA_NAME(@ProcID) + N'].[' + @ErrorProcedure + N']';

IF @ExtraMessage IS NOT NULL
SET @ErrorMessage = @ErrorMessage + NCHAR(13) + NCHAR(10) + @ExtraMessage;

-- Email alert
DECLARE @mailbody nvarchar(4000) =
'Error ' + CAST(@ErrorNumber AS varchar(20)) + ' occurred in procedure '
+ COALESCE(@ErrorProcedure, 'NA') + ' at line ' + CAST(@ErrorLine AS varchar(20))
+ ' with a severity of ' + CAST(@ErrorSeverity AS varchar(20))
+ ' and a state of ' + CAST(@ErrorState AS varchar(20))
+ '.' + CHAR(13) + CHAR(10) + CHAR(13) + CHAR(10)
+ @ErrorMessage;

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'sqladmin'
,@recipients = 'junk@junk.com'
,@importance = 'High'
,@subject = 'Load Exception'
,@body = @mailbody;

---- write to log
--INSERT INTO dbo.Exceptions
--(
-- ExceptionDate, ErrorNumber, ErrorSeverity, ErrorState
-- ,ErrorProcedure, ErrorLine, ErrorMessage
--)
--SELECT CURRENT_TIMESTAMP, @ErrorNumber, @ErrorSeverity, @ErrorState
-- ,COALESCE(@ErrorProcedure, N'NA'), @ErrorLine, COALESCE(@ErrorMessage, N'No Message');

RAISERROR
(
N'Error %d occurred in procedure %s at line %d. %s'
,@ErrorSeverity
,@ErrorState
,@ErrorNumber
,@ErrorProcedure
,@ErrorLine
,@ErrorMessage
);

Go to Top of Page

bingobasketball
Starting Member

4 Posts

Posted - 2014-12-02 : 12:51:28
This is very good piece of code
but it still returns only last error

Thank you for posting it, but I am still stuck

My_Sig_Nature
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-12-02 : 12:55:37
I am not sure how to capture the first error, but you can view it after the backup fails in the SQL Server Error Log. So your first troubleshooting step after receiving the email about the backup failing is to check for the specific error in the Error Log (or Application Log in Event Viewer).

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

bingobasketball
Starting Member

4 Posts

Posted - 2014-12-02 : 14:49:21
Thank you for your help. I will continue to search and will post back here once I find the answer.

My_Sig_Nature
Go to Top of Page
   

- Advertisement -