Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Using SQLFiddle, PostgreSQL 9.3.1.

I am learning to define triggers in PostgreSQL, and after doing some research I've found out the following:

Triggers in Postgres are different from MYSQL. Where in Postgres you must create a function that RETURNS TRIGGER, in MySQL you can just create a trigger. So this is what I've come up with:

On Employee Insert, we want to update Departments Total Salary.

CREATE FUNCTION update_sal() RETURNS TRIGGER AS $$
BEGIN
  IF NEW.dno IS NOT NULL THEN
    UPDATE Department SET Total_sal = total_sal  + NEW.salary
    WHERE department.dno = NEW.dno;
  END IF;
RETURN NULL;
END;
$$ Language plpgsql;

CREATE TRIGGER updateInsert
AFTER INSERT ON Employee
FOR EACH ROW
EXECUTE PROCEDURE update_sal();

And I'm getting the following error:

Schema Creation Failed: ERROR: unterminated dollar-quoted string at or near "$$ 
BEGIN IF NEW.dno IS NOT NULL THEN UPDATE Department 
SET Total_sal = total_sal +NEW.salary WHERE department.dno = NEW.dno":
share|improve this question

2 Answers 2

If you copy-pasted the code, then you've got a simple syntax error: ENDl should be END; in the last-but-one line of the function definition.

Otherwise, it looks good to me.

share|improve this answer
    
Thanks for your comment Christof, however even with the END; changed I am still getting the same error. –  FatalProphet Apr 19 at 2:37
up vote 1 down vote accepted

I've solved the issue thanks to Database Function giving an error - Postgresql

It seems just changing the query terminator at the bottom of the Scheme Window solves this issue.

share|improve this answer

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.