May 15, 2024 at 4:55 pm
Is there a trigger to notify when a new SQL Agent job is created/modified?
May 15, 2024 at 5:46 pm
No, but you can make one and actually keep track of history. I have it show up in my "Morning Report".
Here's the table I created in my "DBA" database...
USE DBA;
GO
CREATE TABLE [dbo].[JobModifiedBy]
(
[Job_ID] [uniqueidentifier] NOT NULL,
[Job_Name] [sysname] NOT NULL,
[Date_Modified] [datetime] NOT NULL,
[Modified_By] [sysname] NOT NULL,
[Program_Name] [sysname] NOT NULL,
[Host_Name] [sysname] NOT NULL
)
GO
CREATE CLUSTERED INDEX [CI_By_Date_ModifiedJob_ID] ON [dbo].[JobModifiedBy]
(
[Date_Modified] ASC,
[Job_ID] ASC
)
GO
And here's the trigger I created in the MSDB database to capture the info.
USE msdb;
GO
CREATE TRIGGER [dbo].[JobModifiedBy] ON [msdb].[dbo].[sysjobs] AFTER UPDATE, INSERT
/**********************************************************************************************************************
Purpose:
To keep a history of who changes jobs and the date/time they made the change.
This trigger populates the "DBA.dbo.JobModifiedBy" table, which will be used by the Morning Job report.
-----------------------------------------------------------------------------------------------------------------------
Revision History:
Rev 00 - 16 Sep 2016 - Jeff Moden - Initial creation and deployment.
**********************************************************************************************************************/
AS
SET NOCOUNT ON
;
IF UPDATE (date_modified)
BEGIN
--===== Insert data from the "inserted" logical table for both INSERT and UPDATE.
INSERT INTO DBA.dbo.JobModifiedBy
(
Job_ID
,Job_Name
,Date_Modified
,Modified_By
,[Program_Name]
,[Host_Name]
)
SELECT JOB_ID
,[name]
,Date_Modified
,suser_name()
,PROGRAM_NAME()
,HOST_NAME()
FROM inserted
;
END
;
GO
If you need to, you could modify the trigger to fire when DELETEs occur, as well.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 15, 2024 at 6:15 pm
Thank you!
May 15, 2024 at 6:58 pm
If I wanted this to email me also when the change was made, how would I do that?
May 16, 2024 at 2:09 am
If I wanted this to email me also when the change was made, how would I do that?
Setup a stored procedure to send the email and have the trigger fire the stored procedure. Email is asynchronous so even if email is toasted, it won't cause an issue.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy