Output who invoked a SQL Job

  • Hi all,

    Is there a way I could output the username of whoever invoked a SQL job each time it runs? ...and perhaps write it to a table?

    I have used suser_name() to output the user that runs each step, but these are all being automatically started by the SQL Agent service account, so obviously that username is all I get.

    Any ideas?

    Thanks,

    Matt

  • You can find this in the job history table.

    SELECT j.name,

    jh.run_date,

    jh.run_time,

    SUBSTRING(jh.message, CHARINDEX('.', jh.message) + 3, CHARINDEX('.', jh.message, CHARINDEX('.', jh.message) + 1) - CHARINDEX('.', jh.message) - 3) AS MiddleMessage

    FROM msdb..sysjobhistory jh

    JOINmsdb..sysjobs j

    ON jh.job_id = j.job_id

    WHEREjh.step_id = 0

    The SQL Guy @ blogspot[/url]

    @SeanPearceSQL

    About Me[/url]

  • That looks good - many thanks!

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

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