Sql-server – MS SQL Server 2014: which stored procedure is the error in

error handlingsql serversql server 2014stored-procedures

As a general question or three on Microsoft SQL Server 2014, that perhaps I should know the answer to already, or even perhaps did once –

When my stored procedures run other stored procedures, how can I find out which stored procedure's code generated the error?

Is there a particular way to handle errors from Transact-SQL that identifies the location of the error in a given stored procedure?

Can program code in a stored procedure obtain the name of the procedure it is in?

I am prepared to put "SET @MyNameIs = N'HastilyWrittenProcedure' " in program code if it's the only way. Then I can put "PRINT @MyNameIs + ' broke.' " in the error handler in everything.

Best Answer

Use @@procid to get the currently executing routine and object_name() to convert the id to a name

Hth