Skip to content
Advertisement

Postgres function to return Table into variables

How can I capture different columns into different variables like so (note this is only pseudocode so I am assuming it will cause errors. Example taken from here)

Advertisement

Answer

Assuming you have some purpose for the variables after retrieving them not just ending the function your “get_film_into_variables” is almost there. But first let’s backup just a bit. A function that returns a table does just that, you can use the results just like a table stored on disk (it just goes away after query or calling function ends). To that end only a slight change to the “get_film_into_variables” function is required. The “get_film” becomes the object of the FROM clause. Also change the returns null, to returns void. So

The above works for a single row returned by a function returning table. However for a return of multiple rows you process the results of the table returning function just lake you would an actual table – with a cursor.

BTW: the get_film function can be turned into a SQL function. See fiddle here. For demo purposes get_film_into_variable routines return a message.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement