user779159
user779159

Reputation: 9602

Postgres plpgsql return table and value

In a plpgsql function I'm doing returns table with a few columns to return a table with a few rows. But now I want to also return an individual scalar value along with it. So one individual scalar value, plus several rows. How can I do this?

An example is

select count(*) from exampletable into individidual_scalar_value;
select a, b, c from anothertable into several_rows limit 10;

Upvotes: 0

Views: 208

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520948

You could use a subquery for the count on the first table:

select a, b, c, (select count(*) from exampletable)
from anothertable
into several_rows
limit 10;

But note that in general, using LIMIT without ORDER BY is fairly meaningless, because you are not telling Postgres which 10 records you want to actually select. So, add a sensible ORDER BY clause to the above query for best results.

Upvotes: 1

Related Questions