2

I have a Postgres function which returns a table "RETURNS TABLE......"

There is some business rules in place in the code. If certain conditions are not met, I want to return just an empty record set. Is there a simple way to do this? Or do I need to select a NULL for each column returned?

Right now if I try to return just NULL, it complains: ERROR: structure of query does not match function result type.

2
  • Without seeing the code it is hard to say. If you are selecting from another table could you just do a SELECT that you know does not return anything? Commented Dec 23, 2020 at 0:12
  • Hi, Welcome to SO. Please ensure you include the Minimum Reproducible Example so that we can ensure we assist you as best as we can. Commented Dec 23, 2020 at 3:36

1 Answer 1

5

Assuming you are using PL/pgSQL, you can use a plain RETURN; statement without an argument to return an empty result set.

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

2 Comments

Thanks! Looks like a RETURN statement by itself will ignore the actual RECORD layout, and just give an empty set. Much appreciated!!!
That ignores nothing. In set returning functions, you return a row with RETURN NEXT; and end the result set with RETURN;.

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.