The simulation implements the exception stack, reports the call chain and the exception location, with the following code:
CREATE PROC [Myhelper]. [ThrowError] @ProcId intas--for the re-encapsulation error, Microsoft has a RAISERROR solution: [SYS]. [Sp_replrethrow] IF Error_number () is NULL return;declare @_procname sysname=isnull (LTRIM (RTRIM (@ProcId)), ' Error location unknown '), object_name Num Int=error_number (), @_msg NVARCHAR (4000) =error_message (), @_line NVARCHAR (+) =isnull (CONVERT (nvarchar,error_ Line ()), ' unknown '), @_state tinyint=error_state (), @_numcaption NVARCHAR (), @_msgcaption NVARCHAR (+), @_severity int= Error_severity (); IF @_num<50000 beginset @_numcaption= ', ErrorNum: ' +convert (Nvarchar,@_num); SET @[email Protected]_num; SET @_msgcaption= ', Message: '; Endelse beginset @_numcaption= "; SET @[email protected]_state+1; SET @_msgcaption= '; ' +char (+) +char (10); Endset @_msg= ' +convert (nvarchar,@_state) + ' layer: ' + ' Procedure: ' [email protected]_procname+ ', line: ' [email protected]_ Line[email protected]_numcaption+ @_msgcaption[email protected]_msg; THROW @_num,@_msg,@_state;
How to use:
BEGIN tryexec sp_executesql N ' SELECT 1 from tbl; '; END trybegin catchexec myhelper.throwerror @ @PROCID; END Catchset NOCOUNT OFF;
MSSQL Exception Handling Framework