原来写一篇在 SQL Server 中用Update 触发器修改值的博客:

https://blog.csdn.net/yenange/article/details/85126707

今天又碰到有个贴子里希望用Update触发器修改值, 区别比较大, 记录下来留底:

##注意点:
## 1. 在客户端中创建触发器,必须使用 delimiter $$
## 2. 只能用 before 触发器
## 3. 触发器中不能用 update ,只能用 set
drop table if exists test;
create table test(
	id int primary key,
	flg nvarchar(20)
);
insert into test values(1,'');
insert into test values(2,'');

drop trigger if exists trig_test_update;
delimiter $$
CREATE TRIGGER `trig_test_update`
before  update  on `test` for each row
BEGIN
  set new.flg='有更新';
END;
$$

update test set flg='a';

select * from test;
/*
+----+--------+
| id | flg    |
+----+--------+
|  1 | 有更新 |
|  2 | 有更新 |
+----+--------+
*/

 

Logo

更多推荐