0

I try to create a procedure that return a query table from Postgresql, but I'm stuck at creating the procedure

CREATE OR REPLACE PROCEDURE "sap_data"."get_emp_number"(dep VARCHAR)
     RETURNS TEMP TABLE (
                    emp_id VARCHAR,
                    department VARCHAR)
     AS $BODY$
     BEGIN
        RETURN QUERY SELECT
            emp_id,
            department
        FROM
            emp_data
        WHERE
            department = dep
    END;$BODY$
      LANGUAGE plpgsql

When I save this procedure, this error occurs:

ERROR: syntax error at or near "TABLE" LINE 2: RETURNS TABLE (

I want to return this as a query so I can query this table using parameter to filter it.

1
  • Did you copy & paste your code? Because there's no space between the quotation mark and the bracket: "get_emp_number"(dep VARCHAR) Commented Nov 1, 2021 at 13:30

1 Answer 1

3

There are several errors in the creation.

  • It is function.
  • Functions can't return a temporal table.
  • You should prefix columns with a table name in RETURN QUERY SELECT.
  • The semicolon before END is needed.

it might look something like this:

CREATE OR REPLACE Function "get_emp_number"(dep VARCHAR)
     RETURNS TABLE (
                    emp_id VARCHAR,
                    department VARCHAR)
     AS $BODY$
     BEGIN
        RETURN QUERY SELECT
            emp_data.emp_id,
            emp_data.department
        FROM
            emp_data
        WHERE
            emp_data.department = dep;
    END;
    $BODY$
    LANGUAGE plpgsql
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.