Home All Groups Group Topic Archive Search About

How to Obtain A Job Name

Author
18 Aug 2005 5:01 PM
Tom Frost
I have a common stored procedure that is called from any number of Jobs and
I'm looking to determine if theres any way that the stored proc using SQL 
can determine the ID of the job which executed it so I  can go out to
MSDB..sysjobs and get the job name and insert it into any error messages that
might come out.

Author
18 Aug 2005 5:56 PM
Aaron Bertrand [SQL Server MVP]
You could add a parameter to the SP, and code it into each job.

I assume you have a logging table or something like it?  For simplicity:

CREATE TABLE joblogging
(
jobname SYSNAME,
dt SMALLDATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
)
GO

Now add this insert to the stored procedure:

ALTER PROCEDURE dbo.yourproc
@jobID UNIQUEIDENTIFIER
AS
BEGIN
/* other stuff */

INSERT joblogging(jobname)
SELECT name
FROM msdb..sysjobs
WHERE job_id = @jobID

END
GO

And then you can use the [JOBID] token in each job:

EXEC yourproc @jobid='[JOBID]'

You can also include the [STEPID] and other variables; see sp_add_jobstep in
Books Online for a complete list.

Yes that's a pain in the butt, but how many jobs are really calling the
exact same stored procedure?




Show quote
"Tom Frost" <TomFr***@discussions.microsoft.com> wrote in message
news:6BB33B1B-5A1C-4CAC-9B64-C6755451F3E0@microsoft.com...
>I have a common stored procedure that is called from any number of Jobs and
> I'm looking to determine if theres any way that the stored proc using SQL
> can determine the ID of the job which executed it so I  can go out to
> MSDB..sysjobs and get the job name and insert it into any error messages
> that
> might come out.
>
>
>
>

AddThis Social Bookmark Button