Within a Postgres function I would like to run a number of SQL SELECT
commands and append the output to the same variable. After all the SELECT
commands have run this output should be written to a file so that the user could read this report.
What is the best way to implement this?
Best Answer
Basic function to return rows from multiple
SELECT
statements:You could do the same with
UNION ALL
.Simple & quick demo to output from tables with different structure:
You can cast any type to
text
in PostgreSQL, even the composite type of a table row ...Call:
To write this to a file (local to the server):
To get you started read these chapters in the manual:
CREATE FUNCTION
COPY
For a more specific answer you need to write a more specific question.