0

I need to learn more about postgres but I am trying to write a function that links to a trigger in my postgres db. Normally the functions I write work fine, however, this particular function keeps returning errors around the syntax at the end of my output.

I have a fixtures table with a column called 'Result'. I want to read the scores from the fixture e.g. Wolves 0:0 Ipswich, and trigger into Result 'Draw'.

The query I am writing is as follows:

CREATE OR REPLACE FUNCTION sppullscoretrigger()
RETURNS trigger

SECURITY DEFINER
AS $BODY$

DECLARE
  payload text;
BEGIN
    SELECT NEW.Home_Side_Score, NEW.Away_Side_Score FROM Fixtures;
        IF    NEW.Home_Side_Score is NULL or NEW.Away_Side_Score is NULL THEN NEW.Result = 'TBC';
        IF NEW.Home_Side_Score = NEW.Away_Side_Score THEN NEW.Result = 'Draw';
        IF NEW.Home_Side_Score > NEW.Away_Side_Score THEN NEW.Result = 'Home_Win';
        IF NEW.Home_Side_Score < NEW.Away_Side_Score THEN NEW.Result = 'Away_Win';
        ELSE NEW.Result = 'Error';
        END IF;


    RETURN NEW;
END
$BODY$
LANGUAGE plpgsql

CREATE TRIGGER trgscore
BEFORE INSERT OR UPDATE
ON Fixtures
FOR EACH ROW
EXECUTE PROCEDURE sppullscoretrigger();

I have only written basic functions before but I am really struggling with getting this trigger to work.

4
  • 1
    You are missing a ; after the trigger function (and before the create trigger). Also: SELECT NEW.Home_Side_Score, NEW.Away_Side_Score FROM Fixtures; will select all rows from the table fixtures but with constant values for each row. You do not need that select at all. In a row level trigger you can just access the new and old records Commented Apr 3, 2016 at 8:55
  • Thank you. I have put the missing semi-colon in and that resolved the issue as well was the ELSIF issue identified by Joachim. Commented Apr 3, 2016 at 9:10
  • I have just tried using this trigger when I insert data and it tells me "Error: record 'New' has no field 'home_side_score' CONTEXT: SQL statement "SELECT New.Home_Side_Score is NULL or New.Away_Sido_Score is NULL"...." Any ideas? I thought the New attribute would hold the data I am importing so there would be a field home_side_score etc. Commented Apr 3, 2016 at 9:19
  • Yes it will. But to find the cause of the error you need to edit your question and add the complete create table statement for the table. But it's probably better to ask a new question for that problem. Commented Apr 3, 2016 at 9:21

1 Answer 1

1

You seem to be using IF when you mean ELSIF. This version compiles;

CREATE OR REPLACE FUNCTION sppullscoretrigger()
RETURNS trigger

SECURITY DEFINER
AS $BODY$

DECLARE
  payload text;
BEGIN
    SELECT NEW.Home_Side_Score, NEW.Away_Side_Score FROM Fixtures;
        IF    NEW.Home_Side_Score is NULL or NEW.Away_Side_Score is NULL THEN NEW.Result = 'TBC';
        ELSIF NEW.Home_Side_Score = NEW.Away_Side_Score THEN NEW.Result = 'Draw';
        ELSIF NEW.Home_Side_Score > NEW.Away_Side_Score THEN NEW.Result = 'Home_Win';
        ELSIF NEW.Home_Side_Score < NEW.Away_Side_Score THEN NEW.Result = 'Away_Win';
        ELSE NEW.Result = 'Error';
        END IF;

    RETURN NEW;
END
$BODY$
LANGUAGE plpgsql

A quick SQLfiddle to test with.

Sign up to request clarification or add additional context in comments.

1 Comment

I have just tried using this trigger when I insert data and it tells me "Error: record 'New' has no field 'home_side_score' CONTEXT: SQL statement "SELECT New.Home_Side_Score is NULL or New.Away_Sido_Score is NULL"...." Any ideas? I thought the New attribute would hold the data I am importing so there would be a field home_side_score etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.