1

I'm new to Postgres and plpsql, so sorry if the answer is obvious. However I wasn't able to figure out what I'm doing wrong here. When I use the sample DB function, everything is fine. When I try to create my own:

CREATE OR REPLACE FUNCTION get_trans (p_param VARCHAR) 
  RETURNS TABLE (
    amount money,
    transactionId UUID
  ) 
  AS $$
  BEGIN
    RETURN QUERY SELECT
      amount,
      transactionId
    FROM
    transactions;
  END; $$ 

LANGUAGE 'plpgsql';

The function executes, but testing it by running the following test statement fails:

SELECT * 
FROM public.get_trans(
  'bs'
)

with this error:

ERROR:  relation "public.transactions" does not exist
LINE 4:  FROM public.transactions
          ^
QUERY:  SELECT
          amount,
          transactionId
FROM public.transactions
CONTEXT:  PL/pgSQL function get_trans(character varying) line 4 at RETURN 
QUERY
SQL state: 42P01

It looks like it's a pretty common error with plpsql, and I realize that I'm probably missing smth obvious, but none of the answers so far helped in my case. is a straight forward stand-along table with only primary key for now.

Any tips are highly appreciated.

UPDATE: So, thanks to LD I found out that my table although created doesn't exist in any schema due to syntax errors. It turned out postgres is more different from MSSQL as I thought, and harder to flash out script errors. The main error was - you can't use camel-case in table or column definition. Once I fixed those and rerun the function, everything as fine.

1
  • The table is probably in a different schema. Either set your search_path or add the schema to the table name in the function definition. Commented Aug 7, 2018 at 6:58

1 Answer 1

1

Function itself seems be ok. Please ma sure the table transactions exists in public schema by using(check the column table_schema in results):

SELECT * 
FROM information_schema.tables 
WHERE  table_name = 'transactions'
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.