Murilo lima alves
Murilo lima alves

Reputation: 55

SQL - Add Date and Time in the error result

Is it possible to add the date and time for every thrown SQL error? Example:

Msg 8134, Level 16, State 1, Line 97 Divide by zero error encountered.

Should be something like

Msg 8134, Level 16, State 1, Line 97, 04/02/2018, 12:20, Divide by zero error encountered.

The only functions I know regarding errors are: ERROR_LINE, ERROR_MESSAGE, ERROR_NUMBER, ERROR_PROCEDURE, ERROR_SEVERITY, ERROR_STATE

Upvotes: 0

Views: 78

Answers (1)

Jimbo
Jimbo

Reputation: 2537

Use the code below as a starting point, add the date and time wherever it suits you:

DECLARE @ErrorNumber INT = ERROR_NUMBER();

DECLARE @ErrorLine INT = ERROR_LINE();

DECLARE @ErrorMessage NVARCHAR(4000) = ERROR_MESSAGE();

DECLARE @ErrorSeverity INT = ERROR_SEVERITY();

DECLARE @ErrorState INT = ERROR_STATE();

PRINT 'Actual error number: ' + CAST(@ErrorNumber AS VARCHAR(10));

PRINT 'Actual line number: ' + CAST(@ErrorLine AS VARCHAR(10));

RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState);

Upvotes: 1

Related Questions