Am trying to Create a function that would loop through all the tables in a database and Create a trigger if it does not exist.After some research i came across the store procedure:"sp_MSforeachtable procedure" Can some one help me with a code that would use the function to create the trigger below.The Trigger works fine just that now i need to make sure it is applied on all table using a loop.NB I don't want to manual run it on all table.Performance wise i know may not be the best idea to have triggers on all tables in a database. here is my trigger
USE [Issue]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[Update_DateCreated_DateModified]
ON [dbo].[Patient]
FOR INSERT
AS
BEGIN
DECLARE @getDateCreated Datetime = GETDATE()
DECLARE @getDateModified DATETIME=GETDATE()
DECLARE @patient_nin VARCHAR(50) = (SELECT NIN FROM INSERTED i)
SET NOCOUNT ON;
UPDATE dbo.Patient SET DateCreated=@getDateCreated,DateModified=@getDateModified
END
GO