When you see a message like below, the first line number on the first line is the line number on which the error was raised. Msg 50000, Level 16, State 1, Line 26Divide by zero error encountered.
The second line is the error message. That does not contain the error line by default. So you need to provide it. So make the change shown in red to your catch block:BEGIN CATCH DECLARE @ErrorMessage nvarchar(4000); DECLARE @ErrorSeverity int; DECLARE @ErrorState int; DeCLARE @ErrorLine int; SELECT @ErrorMessage = ERROR_MESSAGE() +N' and that happened on %d', @ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE(), @ErrorLine = ERROR_LINE(); RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState, @ErrorLine); IF ((XACT_STATE()) = -1) BEGIN ROLLBACK; END ELSE IF ((XACT_STATE()) = 1) BEGIN COMMIT; ENDEND CATCH
In this, the %d is the first token used for string formatting, which you already are providing (as the fourth parameter to the RAISERROR function. The way you have it, the fourth parameter is not even used.Edit: Reading back on what I wrote above, I know I am saying it in the most confusing manner. Take a look at the RAISERROR page and pay particular attention to the first parameter and how it relates to the fourth and subsequent parameters: http://msdn.microsoft.com/en-us/library/ms178592(v=sql.105).aspx