当前位置: 数据库>sqlserver
sql server 触发器实例(判断是插入、删除,还是修改)
来源: 互联网 发布时间:2014-08-29
本文导语: sql server触发器的例子。 sql代码: 代码示例: --触发器实例 --判断是插入、删除还是修改数据 --by www. create TRIGGER [updatetest] ON [dbo].[TEST] FOR INSERT, UPDATE, DELETE AS begin DECLARE @IsInsert bit, @IsUpdate bit, @IsDelete...
sql server触发器的例子。
sql代码:
代码示例:
--触发器实例
--判断是插入、删除还是修改数据
--by www.
create TRIGGER [updatetest] ON [dbo].[TEST]
FOR INSERT, UPDATE, DELETE
AS
begin
DECLARE
@IsInsert bit,
@IsUpdate bit,
@IsDelete bit
IF EXISTS(SELECT 1 FROM inserted) AND NOT EXISTS(SELECT 1 FROM deleted)
SET @IsInsert = 1
ELSE
SET @IsInsert = 0
IF EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted)
SET @IsUpdate = 1
ELSE
SET @IsUpdate = 0
IF NOT EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted)
SET @IsDelete = 1
ELSE
SET @IsDelete = 0
if (@IsUpdate=1)
PRINT 'updated '
if (@IsInsert=1)
PRINT 'insert '
if (@IsDelete=1)
PRINT 'delete '
EDN
--判断是插入、删除还是修改数据
--by www.
create TRIGGER [updatetest] ON [dbo].[TEST]
FOR INSERT, UPDATE, DELETE
AS
begin
DECLARE
@IsInsert bit,
@IsUpdate bit,
@IsDelete bit
IF EXISTS(SELECT 1 FROM inserted) AND NOT EXISTS(SELECT 1 FROM deleted)
SET @IsInsert = 1
ELSE
SET @IsInsert = 0
IF EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted)
SET @IsUpdate = 1
ELSE
SET @IsUpdate = 0
IF NOT EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted)
SET @IsDelete = 1
ELSE
SET @IsDelete = 0
if (@IsUpdate=1)
PRINT 'updated '
if (@IsInsert=1)
PRINT 'insert '
if (@IsDelete=1)
PRINT 'delete '
EDN
说明:
inserted、deleted:
这是两个虚拟表,inserted 保存的是 insert 或 update 之后所影响的记录形成的表,deleted 保存的是 delete 或 update 之前所影响的记录形成的表。