Skip to content
Advertisement

Query has no destination for result data in function PostgreSQL

I have a function that returns results to me, but the result is not what I expected and capturing the error message I get this:

this is my code postgresql:

this is the error message:

I can not find the cause of the error I have tried changing the return like this:

How could I solve it, I really appreciate your help

Advertisement

Answer

A non void function expects a RETURN corresponding to the data type (or structure) declared at RETURNS. In your case a table containing an integer and two character varying. Take a look at the following test function that returns a table with an int and a text column:

Test

Demo: db<>fiddle

A few thoughts:

  • In plpgsql it is possible to have multiple RETURN statements, so that different parts of your code return something different without exiting the function. But in case you prefer to have an intermediate table to collect the information and return it only once in the end, make sure the table is either manually dropped after the function is completed (or if it raises an error!) or simply create it as ON COMMIT DROP. If you prefer the former, take a look at UNLOGGED TABLES.
  • Consider using text over character varying.
  • Your WHILE loop uses an incremental value of 1 until it reaches a certain limit. You could use a FOR loop and get rid of the variable i in the DECLARE clause. It’s no big deal but depending on your function size it might make things cleaner.
  • You can use the parameter order instead of its name, e.g. $1. It is also no big deal but it avoids conflicts and/or confusion with columns that might have the same name.
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement