Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

postgresql

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 ?

like image 490
Sumon Bappi Avatar asked Sep 21 '16 04:09

Sumon Bappi


1 Answers

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 -- Says the function is implemented in the plpgsql language; VOLATILE says the function has side effects.
COST 100; -- Estimated execution cost of the function.
like image 193
chalitha geekiyanage Avatar answered Sep 19 '22 14:09

chalitha geekiyanage