SQL Server Agent - get my own job_id
We had trouble with this recently and did not go the route you found in MSDN. Instead, we recovered the jobid from dbo.sysjobs by name directly (the opposite of your example) and then used that within the job to check execution status (exiting out of long running while loop if job state had changed).
declare @jobid uniqueidentifier
SELECT @jobid = job_id from msdb.dbo.sysjobs where name = '[blah]'
thanks for your answers. The problem is that I tried to parse the statement in job step. Then I got this error. While running the job there's no problem. My very best solution now is:
declare @JobID uniqueidentifier
SELECT @JobID = $(ESCAPE_NONE(JOBID));
PRINT 'My JobID is ' + Convert(char(255), @JobID)
Now you handle with @JobID, but as far as I know until now you have to convert always to char(255). Thanks to user state_dba on MSDN.
Just forget what parser is saying - variable resolution is done at runtime. Parser does not know about that.