title: SQL Server Agent - get my own job_id
body: <p>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.<br>
  On MSDN (<a href="http://msdn.microsoft.com/en-us/library/ms175575(v=SQL.100).aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/ms175575(v=SQL.100).aspx</a>) you can find a description of using tokens in job steps. Wow, great, that's what I'm looking for!! Just use (JOBID).<br>
  Since SQL Server 2005 SP1 you have to use macro like $(ESCAPE_NONE(JOBID)). No problem.<br>
  But if you try the example:  </p>
  
  <pre><code>DECLARE @name NVARCHAR(128)
  select @name =  name from msdb.dbo.sysjobs where job_id = $(ESCAPE_SQUOTE(JOBID))
  PRINT @name
  </code></pre>
  
  <p>you get:<br>
  Incorrect syntax near 'ESCAPE_SQUOTE'. (Microsoft SQL Server, Error: 102)<br>
  Ok, now from the scratch:</p>
  
  <pre><code>PRINT N'$(ESCAPE_SQUOTE(JOBID))'  
  </code></pre>
  
  <p>results in 0xE33FE637C10B3C49A6E958BB3EF06959 but the job_id is 
  37E63FE3-0BC1-493C-A6E9-58BB3EF06959<br>
  The "N'" I think makes an implicit conversion to NVARCHAR of the (JOBID)...<br>
  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):</p>
  
  <pre><code>declare @jobid binary(16)
  SELECT @jobid =Convert(Uniqueidentifier,$(ESCAPE_NONE(JOBID)))
  </code></pre>
  
  <p>results in:<br>
  <em>Incorrect syntax near '('. (Microsoft SQL Server, Error: 102)</em><br>
  I'm totally confused now. Could please someone help me with a good advice or solution. Every kind of help is appreciated.  </p>
  
  <p>Best regards
  Helmut</p>