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
LVL 3
Jon JaquesInformation TechnologistAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
What does SUBSTRING return?

You need to convert it explicitly to varbinary.
0
Jon JaquesInformation TechnologistAuthor Commented:
I'm not sure how to output it in such a way that I can see it. Suggestions?
0
Randy PooleCommented:
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

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Jon JaquesInformation TechnologistAuthor Commented:
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.
0
Randy PooleCommented:
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.
0
Jon JaquesInformation TechnologistAuthor Commented:
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
0
ste5anSenior DeveloperCommented:
E.g.
DECLARE @context_info VARBINARY(128) = CONTEXT_INFO();

SET @context_info = COALESCE(@context_info, 0xEF, 0xEF + SUBSTRING(@context_info, 2, 127));

SET CONTEXT_INFO @context_info;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jon JaquesInformation TechnologistAuthor Commented:
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!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.