Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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 ?

share|improve this question
up vote 1 down vote accepted

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;
share|improve this answer
    
this creates the trigger. thanks a lot. Can you please explain what is the meaning of LANGUAGE & COST – Sumon Bappi Sep 21 '16 at 4:07
1  
postgresql.org/docs/8.4/static/sql-createfunction.html Read the documentation for pgsql create function. They have explained it clearly. – chalitha geekiyanage Sep 21 '16 at 4:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.