Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return multiple rows in PostgreSQL using RECORD?

Tags:

sql

postgresql

I'm trying to create a function which runs an SQL query on multiple tables and outputs the resulting table from the query. The resulting table will have multiple rows. I'm having a lot of difficulty with this and I've read posts which suggest using RETURN NEXT but I haven't been able to get that to work either. From what I understand RECORD can be used because it takes the values of the data fed into it. Here is my code so far:

CREATE OR REPLACE FUNCTION
    most_docs()
RETURNS
    SETOF RECORD
AS $$
DECLARE
    result RECORD;
BEGIN
    CREATE VIEW MOSTDOC AS
    SELECT P.country, COUNT(P.country) AS cnt
    FROM Producer P, Movie M, ProducerMovie PM
    WHERE M.title = PM.title
    AND M.year = PM.year
    AND P.name = PM.name
    AND M.genre = 'Documentary'
    GROUP BY P.country;

    SELECT DISTINCT M.country INTO result
    FROM MOSTDOC M
    WHERE M.cnt = (SELECT MAX(M.cnt)
    FROM MOSTDOC M);

    RETURN result;
END;
$$ LANGUAGE plpgsql;

Any help would be much appreciated. Thanks.

---------- Word in Progress code

CREATE OR REPLACE FUNCTION
    most_docs()
RETURNS
    SETOF RECORD
AS $$
DECLARE
result RECORD
BEGIN
    CREATE VIEW MOSTDOC AS
    SELECT P.country, COUNT(P.country) AS cnt
    FROM Producer P, Movie M, ProducerMovie PM
    WHERE M.title = PM.title
    AND M.year = PM.year
    AND P.name = PM.name
    AND M.genre = 'Documentary'
    GROUP BY P.country;

    RETURN QUERY SELECT DISTINCT M.country
    FROM MOSTDOC M
    WHERE M.cnt = (SELECT MAX(M.cnt)
    FROM MOSTDOC M);
END;
$$ LANGUAGE plpgsql;
like image 428
tvguide1234 Avatar asked Aug 11 '11 23:08

tvguide1234


People also ask

How do I return a record in PostgreSQL?

To return a table from the function, you use RETURNS TABLE syntax and specify the columns of the table. Each column is separated by a comma (, ). In the function, we return a query that is a result of a SELECT statement.

How do I return multiple result sets in PostgreSQL?

Both stored procedures and user-defined functions are created with CREATE FUNCTION statement in PostgreSQL. To return one or more result sets (cursors in terms of PostgreSQL), you have to use refcursor return type.

What is Setof in PostgreSQL?

The return type of the function is setof employee, meaning it is going to return a rowset of employee rows. The body of the function is a very simple SQL statement to generate the output rows. An SRF can be used in place of a table or subselect in the FROM clause of a query.

Can you do loops in PostgreSQL?

Postgresql supports For loop statements to iterate through a range of integers or results from a sequence query. The For loop is used to iterate over a set of numbers or objects.


1 Answers

If i understand your problem correctly, your'e trying to do something like this (for functions that return setof i always use types)

CREATE TYPE frt_test_type AS
   (
    country character varying,
    cnt integer,
    country character varying); /* types may vary */

CREATE OR REPLACE FUNCTION
  RETURNS SETOF frt_test_type AS
$BODY$DECLARE r record;
BEGIN
  for r in     SELECT P.country, COUNT(P.country) AS cnt, 
FROM Producer P, Movie M, ProducerMovie PM
WHERE M.title = PM.title
AND M.year = PM.year
AND P.name = PM.name
AND M.genre = 'Documentary'
GROUP BY P.country;
  loop
      return next r; 
  end loop;
  return; 
END;$BODY$
  LANGUAGE 'plpgsql'
like image 65
ertx Avatar answered Sep 29 '22 18:09

ertx