Find out the calling stored procedure in SQL Server

后端 未结 4 775
温柔的废话
温柔的废话 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 11:49

    You could have proc1 and proc2 pass their names into proc3 as a parameter.

    For example:

    CREATE PROCEDURE proc3
      @Caller nvarchar(128) -- Name of calling proc.
      AS
      BEGIN
        -- Produce error message that includes caller's name.
        RAISERROR ('Caller was %s.', 16,10, @Caller);
      END
      GO
    
      CREATE PROCEDURE proc1
      AS
      BEGIN
        -- Get the name of this proc.
        DECLARE @ProcName nvarchar(128);
        SET @ProcName = OBJECT_NAME(@@PROCID);
        -- Pass it to proc3.
        EXEC proc3 @ProcName
      END
      GO
    
      CREATE PROCEDURE proc2
      AS
      BEGIN
        -- Get the name of this proc.
        DECLARE @ProcName nvarchar(128);
        SET @ProcName = OBJECT_NAME(@@PROCID);
        -- Pass it to proc3.
        EXEC proc3 @ProcName
      END
      GO
    

提交回复
热议问题