SQL Agent Job Name

Posted by John Liu on Thursday, March 23, 2023

At time, you might need to report the actual SQL Agent job name that took the action in your notification. For example, you have a SQL Agent job that send user an email notification and you want to include in the notification the actual job name that sends the notification. You might hardcode the job name in the notification. However, when the job renamed, you need to also remember to update the hardcoded job name in the code as well. You can use following code to get the actual job name at run time inside your code without hardcoding.

SELECT
	name
FROM
	msdb.dbo.sysjobs
WHERE
	job_id = $(ESCAPE_SQUOTE(JOBID))
~~~