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;
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.
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.
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.
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.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With