我想在数据库上创建触发器,首先检查是否存在类似的触发器,如果不存在则创建它。对于数据库中任何表上的任何插入,它会填充该表上的DateCreated和DateModified列。我当前的一个只在一张桌子上做。这是我的代码:
USE [Issue]
GO
/****** Object: Trigger [dbo].[Update_DateCreated_DateModified] Script Date: 04/11/2013 12:07:41 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Nyname>
-- Create date: <Create ,10-04-13,>
-- Description: <Description,Add DateCreate and DateModifid for last records inserted,>
-- =============================================
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)
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for trigger here
UPDATE dbo.Patient SET DateCreated=@getDateCreated,DateModified=@getDateModified
END
GO