1

What am I doing wrong in the following function?

CREATE OR REPLACE FUNCTION extended_sales(area_type varchar, area_code varchar, dpci varchar) RETURNS TABLE(task_id bigint, location_id int)as

$BODY$

BEGIN 
    IF area_type = 1 THEN
        RETURN QUERY select T.task_id, T.location_id from store_price.task T where T.task_payload->>'str_area_type_i'='3';
    ELSE IF area_type = 2 THEN
        RETURN QUERY select T.task_id, T.location_id from store_price.task T where T.due_date < '2017-10-06';
    ELSE
        RETURN QUERY select T.task_id, T.location_id from store_price.task T where task_payload->>'str_area_type_i'='1' and task_payload->>'str_area_c'='7'  and due_date < '2016-11-07';
    END IF;


END

$BODY$ language plpgsql;

When I run the above function it gives the following error

ERROR:  syntax error at end of input
LINE 17: $BODY$ language plpgsql;

Any help is appreciated. Thanks.

3
  • 3
    I didn't try to compile it, but don't you need a semicolon after END? Commented Nov 13, 2017 at 12:14
  • do you need return; before end? Commented Nov 13, 2017 at 12:19
  • @Hambone: No, you don't. That's optional at the end. Commented Nov 13, 2017 at 14:05

1 Answer 1

5

You have two IF statements and one END IF. Use ELSIF if you want to have a single IF statement.

    IF area_type = 1 THEN
        RETURN QUERY select T.task_id, T.location_id from store_price.task T where T.task_payload->>'str_area_type_i'='3';
    ELSIF area_type = 2 THEN
        RETURN QUERY select T.task_id, T.location_id from store_price.task T where T.due_date < '2017-10-06';
    ELSE
        RETURN QUERY select T.task_id, T.location_id from store_price.task T where task_payload->>'str_area_type_i'='1' and task_payload->>'str_area_c'='7'  and due_date < '2016-11-07';
    END IF;
Sign up to request clarification or add additional context in comments.

Comments

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.