判断数据库表dml操作的类型

本文介绍了一种使用SQL触发器来判断数据库中插入、更新和删除等DML操作的方法,并通过创建测试表和触发器来演示这些操作的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 判断数据库dml操作的类型
--演示准备
if object_id(N'testing','U') is not null
drop table testing
go
create table testing
(id
smallint
)
go
if object_id(N'tr_dml_type_testing','TR') is not null
drop trigger tr_dml_type_testing
go
create trigger tr_dml_type_testing on testing
for insert,delete,update
as
if exists (select 1 from inserted) and not exists(select 1 from deleted)
print 'the action is insert'
if exists (select 1 from inserted) and  exists(select 1 from deleted)
print 'the action is update'
if not exists (select 1 from inserted) and  exists(select 1 from deleted)
print 'the action is delete'
--测试
insert into testing select 1

/*
the action is insert

(1 行受影响)
*/
update testing set id=2
/*
the action is update

(1 行受影响)
*/

delete from testing
/*
the action is delete

(1 行受影响)
*/

--删除测试
drop table testing
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值