为数据库中的所有表创建一个触发器

时间:2013-04-11 12:20:14

标签: sql-server-2008 triggers

我想在数据库上创建触发器,首先检查是否存在类似的触发器,如果​​不存在则创建它。对于数据库中任何表上的任何插入,它会填充该表上的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

0 个答案:

没有答案