Link to home
Start Free TrialLog in
Avatar of Jon Jaques
Jon JaquesFlag for United States of America

asked on

SQL Trigger: Prevent recursive execution not working

Hello, I've been trying to work out a trigger for my database, but am still having a problem with a bit of contributed code which prevents the trigger from being executed in a recursive fashion.

The code uses CONTEXT_INFO(), and looks like this:

-- Check to make sure this is not being executed recursively
IF SUBSTRING(CONTEXT_INFO(), 1, 1) = 0xEF
    RETURN;

DECLARE @context_info varbinary(128)
SET @context_info = 0xEF + SUBSTRING(CONTEXT_INFO(), 2, 127) --set first byte but preserve the rest
SET CONTEXT_INFO @context_info

But when I try to insert a record into the database, I get the following error, even though as near as I can tell it is getting the information it needs:

Msg 2743, Level 16, State 3, Procedure tblTripClient__TRG_INS, Line 31
SET CONTEXT_INFO option requires varbinary (128) NOT NULL parameter.
The statement has been terminated.

Can anybody help? Thanks in advance!

--Jon
Avatar of ste5an
ste5an
Flag of Germany image

What does SUBSTRING return?

You need to convert it explicitly to varbinary.
Avatar of Jon Jaques

ASKER

I'm not sure how to output it in such a way that I can see it. Suggestions?
Give this a try
DECLARE @context_info varbinary(128)
Select @context_info=CONTEXT_INFO()
IF SUBSTRING(@context_info, 1, 1) = 0xEF
    RETURN;
--Although the @context_info of the substring should be something else
SET @context_info = 0xEF + SUBSTRING(@context_info, 2, 127) --set first byte but preserve the rest
SET CONTEXT_INFO @context_info

Open in new window

Nope, that gives me the same error:

Msg 2743, Level 16, State 3, Procedure tblTripClient__TRG_INS, Line 31
SET CONTEXT_INFO option requires varbinary (128) NOT NULL parameter.
The statement has been terminated.
post the new code your using, you did change @context_info in 0xEF + SUBSTRING(@context_info, 2, 127) to something that has a value?  Most likely you are trying to assign a null value.
I'm using the exact code modification that you posted:

DECLARE @context_info varbinary(128)
Select @context_info=CONTEXT_INFO()
IF SUBSTRING(@context_info, 1, 1) = 0xEF
    RETURN;
--Although the @context_info of the substring should be something else
SET @context_info = 0xEF + SUBSTRING(@context_info, 2, 127) --set first byte but preserve the rest
SET CONTEXT_INFO @context_info
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bam! There you go Ste5an, you got it! I guess that means I should read up on COALESCE, but that'll have to wait until later... for now, I'm just happy to have this trigger working!

Thanks!