Current job name from within scheduled job

  • Let me explain what I hope to be doing.

    I plan on using the sp_send_dbmail stored proc to send out emails regarding when a scheduled job has started and stopped. Pretty basic stuff.

    What I'd like to know is whether or not I can get the current job name from a system variable or some other way so I can include it in the message and possibly do a few other things as well.

    Assume I have no idea what the scheduled job name is going to be since these are going to be deployed on servers where I have no access.

    I tried searching BOL but didn't really find anything that would help me. Then again my search-fu may be painfully weak today.

    TIA

  • Good news, you can!

    Have a look at SQL Agent Tokens:

    http://msdn.microsoft.com/en-us/library/ms175575.aspx

    I don't remember if name is available straight away, but job ID is and you can look up the name in msdb.dbo.sysjobs if needed.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Sweet! Exactly what I was looking to do!

    Now I can make a stored proc that we can use in any and all jobs that require notifications.

    Thanks!!

  • You're welcome 😀

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply