postgres触发器创建 - 错误:没有指定语言的SQL状态:42P13

时间:2016-09-21 04:01:23

标签: sql postgresql

我是新手来触发。我正在尝试通过以下链接创建触发器 - WHEN NOT MATCHED BY TARGET。但它给出了一些错误。代码块和错误在下面给出::

代码块>>

http://www.postgresqltutorial.com/creating-first-trigger-postgresql/

错误>>

    CREATE OR REPLACE FUNCTION log_last_name_changes()
  RETURNS trigger AS
$BODY$
BEGIN
 IF NEW.last_name <> OLD.last_name THEN
 INSERT INTO employee_audits(employee_id,last_name,changed_on)
 VALUES(OLD.id,OLD.last_name,now());
 END IF;

 RETURN NEW;
END;
$BODY$

有人可以帮我吗?

1 个答案:

答案 0 :(得分:18)

尝试这种方式:

CREATE OR REPLACE FUNCTION log_last_name_changes()
RETURNS trigger AS
$BODY$
BEGIN
IF NEW.last_name <> OLD.last_name THEN
INSERT INTO employee_audits(employee_id,last_name,changed_on)
VALUES(OLD.id,OLD.last_name,now());
END IF;

RETURN NEW;
END;
$BODY$

LANGUAGE plpgsql VOLATILE -- Says the function is implemented in the plpgsql language; VOLATILE says the function has side effects.
COST 100; -- Estimated execution cost of the function.