patternsqlMinor
PostgreSQL Return Joined Results of Select
Viewed 0 times
postgresqljoinedreturnselectresults
Problem
I'm learning PostgreSQL and was wondering if I can put long selects in stored procedures like this one:
It is supposed to return select from two joined tables.
It gives the following error:
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 statementSolution
Use
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.
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.
Context
StackExchange Database Administrators Q#31251, answer score: 2
Revisions (0)
No revisions yet.