0

What I am looking for is what I would call a "parametrised view". When looking around on the internet, I learned that that is not supported by PostgreSQL, and that I should use a function instead.

So I create a function:

CREATE OR REPLACE FUNCTION public.student_get_byschool(
    _schoolid integer
) RETURNS SETOF student AS
$body$
    SELECT * FROM student WHERE schoolid=_schoolid;
$body$
LANGUAGE 'sql';

However, if I want to select a little more of that student by joining other records, my RETURNS clause isn't correct anymore. I could fix that in two ways:

  1. Use RETURNS SETOF RECORD, but that seems to have some issues in my C#.NET application, as it doesn't give me any column definitions.
  2. Create a new type that holds all the data that I want to return, but that introduces redundancy which is what I very much want to avoid in my database design.

My question now is, are these the only options, or is more possible. Note that I just started PostgreSQL and C#.NET.

1
  • Use returns table (col1 integer, ...) Commented Mar 8, 2013 at 14:16

1 Answer 1

1

As per the documentation for CREATE FUNCTION you can use OUT parameters or RETURNS TABLE for this. RETURNS TABLE tends to be more readable.

This Postgres Online article may prove to be interesting, though I haven't read it in detail to check it and make sure it's up to date and correct.

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

3 Comments

Yes, I also found RETURNS TABLE, and even though it is all in one definition, there is still some redundancy. I am leaning towards views with WHERE clauses now.
@BartFriederichs If the number of variants is small, that can be a good choice - and views sometimes inline better, too. If you use functions, use plain SQL functions and do not mark them STRICT if you want them to be able to inline. Some redundancy is unfortunately necessary because Pg needs to know the output type of the query in the function before it can actually check the function body; it's one of the costs of being able to support a variety of pluggable procedural languages.
thanks for the info, I'll keep it in mind. I was also checking of my ideas were valid obviously.

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.