Sumon Bappi Sumon Bappi - 2 months ago 28
SQL Question

postgres trigger creation - ERROR: no language specified SQL state: 42P13

I am new to trigger. I am trying to create a trigger by following this link -

http://www.postgresqltutorial.com/creating-first-trigger-postgresql/
. But it gives some error. The code block and the error is given below ::

code block >>

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$


And the error >>

ERROR: no language specified
SQL state: 42P13


Can anyone help me please ?

Answer

Try this way:

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
COST 100;
Comments