Postgres触发器创建-错误:未指定语言SQL状态:42P13

z9smfwbn  于 2022-10-15  发布在  PostgreSQL
关注(0)|答案(1)|浏览(324)

我对触发器还是个新手。我正在尝试通过以下链接创建触发器-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$

错误是:

ERROR: no language specified
SQL state: 42P13

下一步我可以尝试什么?

trnvg8h3

trnvg8h31#

尝试这样做:

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.

相关问题