Monday, February 20, 2012

q: SP name in the trigger

There are two different Stored Procedures insert data into a table, is it
possible to catch what stored procedure name was that trigged the trigger in
the trigger?Hi Jim
I don't know of an easy way, but you should be able to use the context_info
such as
CREATE TABLE MyTab ( id int not null identity, val int )
CREATE PROCEDURE sprInsert1
AS
BEGIN
DECLARE @.value varbinary(128)
SET @.value = CAST('sprInsert1' AS varbinary(128))
SET CONTEXT_INFO @.value
INSERT INTO MyTab ( val ) VALUES ( 1 )
END
CREATE PROCEDURE sprInsert2
AS
BEGIN
DECLARE @.value varbinary(128)
SET @.value = CAST('sprInsert2' AS varbinary(128))
SET CONTEXT_INFO @.value
INSERT INTO MyTab ( val ) VALUES ( 2 )
END
CREATE TRIGGER TrgMyTabInsert ON MYTab
FOR INSERT
AS
BEGIN
SELECT CAST(context_info AS varchar(10))
FROM SYSPROCESSES
WHERE SPID = @.@.SPID
END
EXEC sprInsert1
GO
EXEC sprInsert2
GO
EXEC sprInsert2
GO
EXEC sprInsert1
EXEC sprInsert2
EXEC sprInsert2
GO
John
"JIM.H." wrote:

> There are two different Stored Procedures insert data into a table, is it
> possible to catch what stored procedure name was that trigged the trigger
in
> the trigger?|||Hi Jim
I don't know of an easy way, but you should be able to use the context_info
such as
CREATE TABLE MyTab ( id int not null identity, val int )
CREATE PROCEDURE sprInsert1
AS
BEGIN
DECLARE @.value varbinary(128)
SET @.value = CAST('sprInsert1' AS varbinary(128))
SET CONTEXT_INFO @.value
INSERT INTO MyTab ( val ) VALUES ( 1 )
END
CREATE PROCEDURE sprInsert2
AS
BEGIN
DECLARE @.value varbinary(128)
SET @.value = CAST('sprInsert2' AS varbinary(128))
SET CONTEXT_INFO @.value
INSERT INTO MyTab ( val ) VALUES ( 2 )
END
CREATE TRIGGER TrgMyTabInsert ON MYTab
FOR INSERT
AS
BEGIN
SELECT CAST(context_info AS varchar(10))
FROM SYSPROCESSES
WHERE SPID = @.@.SPID
END
EXEC sprInsert1
GO
EXEC sprInsert2
GO
EXEC sprInsert2
GO
EXEC sprInsert1
EXEC sprInsert2
EXEC sprInsert2
GO
John
"JIM.H." wrote:

> There are two different Stored Procedures insert data into a table, is it
> possible to catch what stored procedure name was that trigged the trigger
in
> the trigger?

No comments:

Post a Comment