判断触发器正在处理的是插入,删除还是更新触发
时间:2024-01-19 02:03:28
但是有时候,可以视看处进逻辑程度,可以把三者写成一个触发器,只是在其中稍作判断而已。
你可以根据从下面方法判断触发器是是处理了插入,删除还是更新触发的:
--宣告两个变量
DECLARE @D BIT = 0
DECLARE @I BIT = 0
--如果在DELETED内部临时触发表找到记录,说明旧数据被删除
IF EXISTS(SELECT TOP 1 1 FROM DELETED)
SET @D = 1
--如果在INSERTED内部临时触发表找到记录,说明有新数据插入
IF EXISTS(SELECT TOP 1 1 FROM INSERTED)
SET @I = 1
--如果两个表都有记录,说明触发器是执行更新触发
IF @I = 1 AND @D = 1
PRINT(N'更新。')
--如果变量@I值被变更为1,而变量@D没有变更,说明触发器是执行插入触发
IF @I = 1 AND @D = 0
PRINT(N'插入')
--下面判断成立,说明说明触发器是执行删除触发
IF @I = 0 AND @D = 1
PRINT(N'删除')
另外有关两个内部临时触发表,触发器的Inserted表和Deleted表
触发器有两个虚拟表,Inserted表和Deleted表,这两个表在不同操作情况之下,表中的数据状态可不一样。
一、插入操作(INSERT)时:Inserted表有数据,Deleted表无数据。
二、更新操作(UPDATE)时:Inserted表有数据(新数据),Deleted表有数据(旧数据)。
三、删除操作(DELETE)时:Inserted表无数据,Deleted表有数据。
标签:触发器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
处理及遍历XML文档DOM元素属性及方法整理
2024-05-13 10:39:38
python通过get,post方式发送http请求和接收http响应的方法
2022-04-09 04:37:54
解析mysql修改为utf8后仍然有乱码的问题
2024-01-14 14:36:09
MySQL查询重复数据(删除重复数据保留id最小的一条为唯一数据)
2024-01-14 14:15:33
![](https://img.aspxhome.com/file/2023/6/122346_0s.png)
python opencv之SIFT算法示例
2023-12-27 21:42:33
![](https://img.aspxhome.com/file/2023/0/65950_0s.png)
Python中pip更新和三方插件安装说明
2021-02-07 13:22:39
![](https://img.aspxhome.com/file/2023/1/134451_0s.png)
Python中的 ansible 动态Inventory 脚本
2022-10-23 07:53:08
![](https://img.aspxhome.com/file/2023/8/65028_0s.png)
详解torch.Tensor的4种乘法
2023-08-11 21:09:03
python+pyqt5编写md5生成器
2022-03-11 03:06:47
![](https://img.aspxhome.com/file/2023/4/131434_0s.jpg)
swiper Scrollbar滚动条组件详解
2024-04-19 10:03:28
![](https://img.aspxhome.com/file/2023/2/135922_0s.jpg)
Python sklearn中的K-Means聚类使用方法浅析
2022-03-16 22:01:16
![](https://img.aspxhome.com/file/2023/1/118011_0s.png)
Python应用实现双指数函数及拟合代码实例
2023-04-14 18:17:14
![](https://img.aspxhome.com/file/2023/0/95270_0s.png)
对python 多个分隔符split 的实例详解
2022-12-26 11:24:27
python中dot函数运算过程总结
2021-03-01 16:52:23
![](https://img.aspxhome.com/file/2023/1/130561_0s.png)
Python检测数据类型的方法总结
2022-08-29 22:07:58
![](https://img.aspxhome.com/file/2023/1/65461_0s.png)
Python实现在线音乐播放器
2021-08-24 04:29:12
![](https://img.aspxhome.com/file/2023/3/79683_0s.jpg)
win10+RTX3050ti+TensorFlow+cudn+cudnn配置深度学习环境的方法
2023-02-12 14:02:00
![](https://img.aspxhome.com/file/2023/1/132291_0s.png)
http通过StreamingHttpResponse完成连续的数据传输长链接方式
2023-06-04 03:33:45
python使用openpyxl库读写Excel表格的方法(增删改查操作)
2021-11-29 01:22:43
![](https://img.aspxhome.com/file/2023/3/135113_0s.png)
Vue插槽slot详细介绍(对比版本变化,避免踩坑)
2024-05-13 09:13:39
![](https://img.aspxhome.com/file/2023/7/126447_0s.png)