postgresql 以下触发器为什么不能达到更新某行时时间自动更新的目的,执行update后时间没有变化
CREATETRIGGERupdate_emp_idafterupdateONempFOREACHROWEXECUTEPROCEDUREup_emp_time();CRE...
CREATE TRIGGER update_emp_id
after update
ON emp
FOR EACH ROW
EXECUTE PROCEDURE up_emp_time();
CREATE or Replace FUNCTION up_emp_time() RETURNS trigger AS $up_emp_time$
BEGIN
IF( TG_OP='update' ) then
update emp set update_time = to_char(now(), 'yyyy/mm/dd hh24:mi:ss'::text);
end if;
return null;
END;
$up_emp_time$ LANGUAGE plpgsql; 展开
after update
ON emp
FOR EACH ROW
EXECUTE PROCEDURE up_emp_time();
CREATE or Replace FUNCTION up_emp_time() RETURNS trigger AS $up_emp_time$
BEGIN
IF( TG_OP='update' ) then
update emp set update_time = to_char(now(), 'yyyy/mm/dd hh24:mi:ss'::text);
end if;
return null;
END;
$up_emp_time$ LANGUAGE plpgsql; 展开
2个回答
展开全部
create table emp(id int, info text, update_time timestamp, crt_time timestamp default now());
insert into emp (id,info,update_time,crt_time) values (1,'digoal',now(),now());
postgres=# select * from emp;
id | info | update_time | crt_time
----+--------+--------------------------+--------------------------
1 | digoal | 2013-05-15 19:58:39.2719 | 2013-05-15 19:58:39.2719
(1 row)
CREATE or Replace FUNCTION up_emp_time() RETURNS trigger AS $up_emp_time$
BEGIN
NEW.update_time=now();
return NEW;
END;
$up_emp_time$ LANGUAGE plpgsql;
CREATE TRIGGER update_emp_id
before update
ON emp
FOR EACH ROW
EXECUTE PROCEDURE up_emp_time();
postgres=# update emp set info='new' where id=1;
UPDATE 1
postgres=# select * from emp;
id | info | update_time | crt_time
----+------+----------------------------+--------------------------
1 | new | 2013-05-15 20:00:28.923988 | 2013-05-15 19:58:39.2719
更多PostgreSQL 问题请参考:
http://blog.163.com/digoal@126/
http://blog.163.com/digoal@126/blog/static/1638770402013283547959/
insert into emp (id,info,update_time,crt_time) values (1,'digoal',now(),now());
postgres=# select * from emp;
id | info | update_time | crt_time
----+--------+--------------------------+--------------------------
1 | digoal | 2013-05-15 19:58:39.2719 | 2013-05-15 19:58:39.2719
(1 row)
CREATE or Replace FUNCTION up_emp_time() RETURNS trigger AS $up_emp_time$
BEGIN
NEW.update_time=now();
return NEW;
END;
$up_emp_time$ LANGUAGE plpgsql;
CREATE TRIGGER update_emp_id
before update
ON emp
FOR EACH ROW
EXECUTE PROCEDURE up_emp_time();
postgres=# update emp set info='new' where id=1;
UPDATE 1
postgres=# select * from emp;
id | info | update_time | crt_time
----+------+----------------------------+--------------------------
1 | new | 2013-05-15 20:00:28.923988 | 2013-05-15 19:58:39.2719
更多PostgreSQL 问题请参考:
http://blog.163.com/digoal@126/
http://blog.163.com/digoal@126/blog/static/1638770402013283547959/
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-05-14
展开全部
PostgreSQL 默认,大小写敏感的。
TG_OP='update'
修改为
TG_OP = 'UPDATE'
看看?
TG_OP='update'
修改为
TG_OP = 'UPDATE'
看看?
更多追问追答
追问
UPDATE时报错,超过堆叠深度限制
追答
哦, 你这个是 emp 的 update 触发器里面, 更新 emp 表啊
那就不要用 那个
update emp set update_time = to_char(now(), 'yyyy/mm/dd hh24:mi:ss'::text);
语句了。
修改为:
NEW.update_time := to_char(now(), 'yyyy/mm/dd hh24:mi:ss'::text);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询