Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres calling a void function

I have a void plsql function:

CREATE OR REPLACE FUNCTION do_something(p_id BIGINT) 
RETURNS void
AS $$
BEGIN
   insert ....
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;

I want to call this function from another plsql function, but inside a query like:

SELECT  do_something(m.id) 
FROM    MyTable m
WHERE   m.IsActive;

When I run the sproc i get:

ERROR: query has no destination for result data HINT: If you want to discard the results of a SELECT, use PERFORM instead.

Is it possible to call a void function as part of a select statement?

like image 821
ozczecho Avatar asked Apr 02 '14 07:04

ozczecho


People also ask

Can we call a function inside a function in PostgreSQL?

In EDB Postgres Advanced Server 9.6, you can write a nested function or procedure within: Function. Procedure.

What is void in PostgreSQL?

Alternatively, if you want to define a SQL function that performs actions but has no useful value to return, you can define it as returning void.


1 Answers

HINT: If you want to discard the results of a SELECT, use PERFORM instead.

PERFORM  do_something(m.id) 
FROM    MyTable m
WHERE   m.IsActive;

PERFORM is the PL/PgSQL alternative for SELECT for when you want to throw the results away. It cannot be used outside PL/PgSQL. For more information, see the manual.

like image 154
Craig Ringer Avatar answered Sep 22 '22 11:09

Craig Ringer