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
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