创建 AFTER DML 触发器

-----------创建 AFTER DML 触发器----------

CREATE TRIGGER [ scema_name . ]trigger_name ----可选的架构拥有者以及必需的用户定义的新触发器的名字

on table ----------应用触发器的表名

--------允许你指定ENCRYPTION和/EXECUTE AS子句。ENCRYPTION回加密T-SQL的定义
[ WITH <dml_trigger_option>[...,n]]

AFTER 
{
[INSERT][,][UPDATE] [,][DELETE]
}
[NOT FOR REPLICATION]

AS{sql_statement[...n]} ------一个触发器允许多个T-SQL语句

 

--跟踪所有的插入、更新和删除--

CREATE TABLE Production.ProductInventoryAudit

( ProductID int NOT NULL,
LocationID smallint NOT NULL,
Shelf nvarchar(10) NOT NULL,
Bin tinyint NOT NULL,
Quantity smallint NOT NULL,
rowguid uniqueidentifier NOT NULL,
ModifiedDate datetime NOT NULL,
InsOrUPD char(1) NOT NULL
)


GO


--创建触发器来填充Production.ProductInventoryAudit表

CREATE TRIGGER Production.trg_uid_ProductInventoryAudit

ON Production.ProductInventory
AFTER INSERT,DELETE
AS


SET NOCOUNT ON

--插入的行--
INSERT Production.ProductInventoryAudit
(
ProductID,LocationID,Shelf,Bin,Quantity,
rowguid,ModifiedDate,InOrUPD
)

SELECT DISTINCT i.Location,i.ProductID,i.Bin,i.Shelf,i.Quantity,i.rowguid,GETDATE(),'I' FROM inserted i


--删除的行--
INSERT Production.ProductInventoryAudit
(
ProductID,LocationID,Shelf,Bin,Quantity,
rowguid,ModifiedDate,InOrUPD
)
SELECT DISTINCT D.Location,D.ProductID,D.Bin,D.Shelf,D.Quantity,D.rowguid,GETDATE(),'D' FROM DELETED D

GO

--插入一个新行--
INSERT Production.ProductInventory
(ProductID,LocationID,Shelf,Bin,Quantity)
VALUES(316,6,'A',4,22)

--检测审核表--

 

转载于:https://www.cnblogs.com/siyunianhua/archive/2011/12/26/2302668.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值