Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a trigger which deals with some data for logging purposes like so:

CREATE TRIGGER trgDataUpdated
   ON tblData FOR UPDATE
AS 
BEGIN
    INSERT INTO tblLog ( ParentID, OldValue, NewValue, UserID )
    SELECT  deleted.ParentID, deleted.Value, inserted.Value, 
            @intUserID -- how can I pass this in?
    FROM    inserted INNER JOIN deleted ON inserted.ID = deleted.ID
END

How can I pass in the variable @intUserID into the above trigger, as in the following code:

DECLARE @intUserID int
SET @intUserID = 10

UPDATE tblData
SET    Value = @x

PS: I know I can't literally pass in @intUserID to the trigger, it was just used for illustration purposes.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
297 views
Welcome To Ask or Share your Answers For Others

1 Answer

I use SET CONTEXT_INFO for this kind of action. That's a 2008+ link, prior link has been retired.

On SQL Server 2005+, you'd have CONTEXT_INFO to read it but otherwise you have to get from context_info column in dbo.sysprocesses.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...