Home > Database > Mysql Tutorial > body text

sql update 触发器 可获得被update的行的信息

WBOY
Release: 2016-06-07 18:01:02
Original
1067 people have browsed it

sql update 触发器 可获得被update的行的信息,需要的朋友可以参考下。

代码如下:
create trigger TgName
on tb
for update
as
if update(recommend)
begin
update tb set commenddate=(getdate()) from tb inner join inserted on tb.vlistid=Inserted.vlistid
end

recommend表示被更新的字段.
关键在于Inserted表
触发器语句中使用了两种特殊的表:deleted 表和 inserted 表。
Deleted 表用于存储 DELETE 和 UPDATE 语句所影响的行的复本。在执行 DELETE 或 UPDATE 语句时,行从触发器表中删除,并传输到 deleted 表中。Deleted 表和触发器表通常没有相同的行。

Inserted 表用于存储 INSERT 和 UPDATE 语句所影响的行的副本。在一个插入或更新事务处理中,新建行被同时添加到 inserted 表和触发器表中。Inserted 表中的行是触发器表中新行的副本。

1.插入操作(Insert)
Inserted表有数据,Deleted表无数据

2.删除操作(Delete)
Inserted表无数据,Deleted表有数据

3.更新操作(Update)
Inserted表有数据(新数据),Deleted表有数据(旧数据)

生前何必久睡,死后自会长眠!
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!