I have a function called "getList(date)". This function returns me a items list (with several columns) from the date inputted in the parameter.
If I call:
SELECT * FROM getList('12/31/2014');
It works fine. It returns me a list with the date, the item name and the price.
Something like this:
date item_description price ----------------------------------------------- 12/31/2014 banana 1 12/31/2014 apple 2.5 12/31/2014 coconut 3
But I have another table with the dates that I want to search for.
So, I want to select all the dates from that table and, for each row returned, I want to call my function "getList" to have a result like this:
date item_description price ----------------------------------------------- 12/28/2014 banana 0.5 12/28/2014 apple 1.5 12/28/2014 coconut 2 12/31/2014 banana 1 12/31/2014 apple 2.5 12/31/2014 coconut 3
I don't know exactly how to do it. Of course my data is not a fruit list. This is just to explain the whole thing easier.
Thank you very much.
The normal syntax to call another PL/pgSQL function from within PL/pgSQL is to either reference the function in a SQL SELECT statement, or during the assignment of a variable. For example: SELECT function_identifier ( arguments ); variable_identifier := function_identifier ( arguments );
The heart of all SQL queries is the SELECT command. SELECT is used to build queries (also known as SELECT statements). Queries are the only SQL instructions by which your data can be retrieved from tables and views.
It can be used to replace single quotes enclosing string literals (constants) anywhere in SQL scripts. The body of a function happens to be such a string literal. Dollar-quoting is a PostgreSQL-specific substitute for single quotes to avoid escaping of nested single quotes (recursively).
The OUT parameters are declared as a part of the argument list and are returned as a part of the result. The OUT parameters are very useful in functions that require returning multiple values. They act like uninitialized variables. Unlike the IN parameters, a value must be assigned to the OUT parameters.
LATERAL
joinThe correct way to do this is with a lateral query (PostgreSQL 9.3 or newer):
SELECT d."date", f.item_description, f.price FROM mydates d, LATERAL getList(d."date") f;
See the manual.
SELECT
In older versions you must use a PostgreSQL extension with some ... quirky ... properties, support for set-returning functions in the SELECT
-list. Do not use this unless you know you must support PostgreSQL 9.2 or older.
SELECT d."date", (getList(d."date").* FROM mydates d;
This may result in multiple-evaluation of the getList
function, once for each column of the output.
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