Skip to content
Advertisement

Stored procedure can not be called due to syntax error

We migrated from SQL Server to Postgres and I am trying to rewrite a stored procedure. The procedure is created correctly, but I can not call it.

This is my procedure:

the table-valued-function tvf_commaSeperatedStringToTable just takes a string, splits it and returns a table with all of the different ids and a rownumber. It works just fine and is tested, no errors inside here.

Now when I try to execute it like this

I get this output:

ERROR: query has no destination for result data

HINT: If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT: PL/pgSQL function spr_getItems(integer,character varying,character varying,character varying) line 3 at SQL statement
SQL state: 42601

But I do NOT want to discard the result, I want to see them.

So I tried calling it with select

and then I get this syntax error:

ERROR: syntax error at or near “0”
LINE 2: 0,
^
SQL state: 42601
Character: 40

I also tried executing it in several other way eg by adding the “public.” before the procedures name, but then there has been a syntax error at the “.”. Or with just using select spr_getItems(0, null, null, null) or select spr_getItems(0), select * from call spr_getItems (0) and so on and so forth.

Am I doing something completely wrong and overlooked something in the documentation?

Thanks for any help!

Edit: clarification that I want to see the results
Edit2: accidentally copied a wrong function name
Edit3: added complete body as suggested

Advertisement

Answer

That’s not how Postgres works. Procedures aren’t meant to return result sets.

If you want that use a set returning function:

You also don’t need PL/pgSQL for a simple query encapsulation, language sql will do just fine.

Then use it like a table:

Note that I guessed the data types in the returns table (...) part, you will have to adjust that to the real types used in your tables.


You don’t need the sub-selects to handle the comma separated values either.

E.g. this:

can be simplified to

But passing multiple values as a comma separated string is bad coding style to begin with. You should declare those parameters as array and pass proper arrays to the function.

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