Find out the calling stored procedure in SQL Server

后端 未结 4 772
温柔的废话
温柔的废话 2020-12-20 11:16

Is it possible to find out who called a store procedure?

For example, say I get an error in proc3. From within that proc I want to know if it was calle

4条回答
  •  遥遥无期
    2020-12-20 12:16

    Do you need to know in proc3 at runtime which caused the error, or do you just need to know while debugging?

    You can use SQL Server profiler if you only need to do it during debugging/monitoring.

    Otherwise in 2005 I don't believe you have the ability to stack trace.

    To work around it you could add and extra parameter to proc3, @CallingProc or something like that.

    OR you could add try catch blocks to proc1 and proc2.

    BEGIN TRY
    EXEC Proc3
    END TRY
    BEGIN CATCH
    SELECT 'Error Caught'
    SELECT
        ERROR_PROCEDURE()
    END CATCH
    

    Good reference here : http://searchsqlserver.techtarget.com/tip/1,289483,sid87_gci1189087,00.html

    and of course always SQL Server Books Online

    SQL Server 2008 does have the ability to debug through procedures however.

提交回复
热议问题