1

I have this code in a job step, the stored procedure runs fine, the issue is with the getting of the job name.

declare @jobName varchar(100)

set @jobName = (select name from msdb..sysjobs where job_id = '$(ESCAPE_SQUOTE(JOBID))')

EXEC spSendSuccessEmail @jobName

Am I missing anything?

All I'm trying to do is pass the job id that the step is in to the stored procedure.

1
  • What does '$(ESCAPE_SQUOTE(JOBID))' get resolved to? Maybe try logging that to a table. Nov 27, 2012 at 14:34

1 Answer 1

1

You are clearly using a varchar where a uniqueidentifier is expected. Ideally you want to fix the typing, but a cast will do the trick too. If the sp is expecting a uniqueidentifier try casting @jobName to uniqueidentifier before executing sp

8
  • That won't work. You can't CAST inline in an exec. Also if the implicit cast to unique identifier fails there must be something wrong with the value that an explicit cast won't solve. The OP is trying to use a SQL Agent macro Nov 27, 2012 at 14:32
  • Ok, edited answer, then cast at "set @ jobname = CAST(xxx AS unique identifier). I mentioned in my answer this is a band-aid, a better solution would be to type @jobName as uniqueidentifier.
    – HackyStack
    Nov 27, 2012 at 14:42
  • It isn't the EXEC that is even causing the problem. It is the where job_id = '$(ESCAPE_SQUOTE(JOBID))' Nov 27, 2012 at 14:44
  • the issue is with what @MartinSmith said. i got passed this by setting the job id to what it actually is but i would prefer doing the right way and using the token because now i have to manually change the id for each of the jobs i wanna do this with.
    – JJ.
    Nov 27, 2012 at 15:00
  • 1
    You suggested casting the job name to uniqueidentifier in an irrelevant part of the code and using invalid syntax. Additionally casting on its own is not sufficient and would just give the same error. the OP needs ESCAPE_NONE instead of ESCAPE_SQUOTE (in which case select name from msdb..sysjobs where job_id = $(ESCAPE_NONE(JOBID)) would also work fine with the implicit cast. Nov 27, 2012 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.