We are trying to debug why SQL Agent Job history is getting deleted every 8 hours. In reading the following article, it suggests many great ideas like:
-
Create a trigger to check what application is deleting the job history
-
Check to see if we have a Job that is perhaps purging the job history
-
Check if the sp_purge_jobhistory stored procedure in msdb database is being used manually to delete the SQL Server agent job history
In the article, the trigger they created returned the following as the "Application Name" value:
- SQLAgent – Step History Logger
- SQLAgent – Job Manager
How would I create a trigger like this?
Best Answer
Here is the code to create an audit table and an audit trigger containing the information you are requesting. I have tested this and it gives me the value
SQLAgent - Job Manager
under the columnAppName
. This logs consistently when jobs run and it deletes out data in my history. (I changed it to keep 2 rows in the history to force it to delete to test.)So the function
APP_NAME()
is really what you were looking for.You can find more information related to this by BateTech on Stack Exchange.