Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Agent - get my own job_id

I'm running a SQL Server 2008 64 bit Developer Edition with Service Pack 1 installed. I have a SQL Server Agent Job. Within this job I want to get the job_id of my own job.
On MSDN (http://msdn.microsoft.com/en-us/library/ms175575(v=SQL.100).aspx) you can find a description of using tokens in job steps. Wow, great, that's what I'm looking for!! Just use (JOBID).
Since SQL Server 2005 SP1 you have to use macro like $(ESCAPE_NONE(JOBID)). No problem.
But if you try the example:

DECLARE @name NVARCHAR(128)
select @name =  name from msdb.dbo.sysjobs where job_id = $(ESCAPE_SQUOTE(JOBID))
PRINT @name

you get:
Incorrect syntax near 'ESCAPE_SQUOTE'. (Microsoft SQL Server, Error: 102)
Ok, now from the scratch:

PRINT N'$(ESCAPE_SQUOTE(JOBID))'  

results in 0xE33FE637C10B3C49A6E958BB3EF06959 but the job_id is 37E63FE3-0BC1-493C-A6E9-58BB3EF06959
The "N'" I think makes an implicit conversion to NVARCHAR of the (JOBID)...
Ok, I think I have to care about the datatype of (JOBID). In the book "SQL Server 2008 Administration" on page 168/169 there's also an example of using (JOBID):

declare @jobid binary(16)
SELECT @jobid =Convert(Uniqueidentifier,$(ESCAPE_NONE(JOBID)))

results in:
Incorrect syntax near '('. (Microsoft SQL Server, Error: 102)
I'm totally confused now. Could please someone help me with a good advice or solution. Every kind of help is appreciated.

Best regards Helmut

like image 410
Helmut Rothtauscher Avatar asked Nov 18 '10 10:11

Helmut Rothtauscher


2 Answers

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]'
like image 185
Patrick Farrell Avatar answered Sep 28 '22 05:09

Patrick Farrell


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.

like image 37
Helmut Rothtauscher Avatar answered Sep 28 '22 07:09

Helmut Rothtauscher