I'm learning PostgreSQL and was wondering if I can put long selects in stored procedures like this one:
CREATE OR REPLACE FUNCTION api.book_list()
RETURNS TABLE (
id BIGINT,
published DATE,
category VARCHAR,
author VARCHAR,
name VARCHAR
) AS $$
BEGIN
SELECT
b.id,
b.published,
c.name AS category,
b.author,
b.name
FROM
schemas.book AS b
JOIN
schemas.category AS c
ON
c.id = b.category_id;
END;
$$
LANGUAGE plpgsql;
It is supposed to return select from two joined tables.
It gives the following error:
select * from api.book_list();
ERROR: query has no destination for result data
HINT: If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT: PL/pgSQL function "book_list" line 3 at SQL statement
Best Answer
Use
RETURN QUERY SELECT ...;
.PostgreSQL is complaining because you aren't specifying what to return. Also, since you aren't actually passing parameters to the function this could also be accomplished by a view.