Alex Rivera | Logout

How do I write a postgres stored procedure that doesn't return anything?

Asked 2009-08-27T21:55:31.033
13

How do I write a simple stored procedure in postgres that doesn't return a value at all? Even with a void return type when I call the stored procedure I get a single row back.

CREATE FUNCTION somefunc(in_id bigint) RETURNS void AS $$
BEGIN
   DELETE from test_table where id = in_id;
END;
$$ LANGUAGE plpgsql;
Edit
Report

1 Answer

6

You can achieve "nothing returned" by abusing set-returning functions:

Simple function:

create function x () returns setof record as $$
begin
return;
END;
$$ language plpgsql;

Now you can:

# select x();
 x
---
(0 rows)

In case it doesn't work for you (sorry, I'm using 8.5), try with this approach:

# create function x (OUT o1 bool, OUT o2 bool) returns setof record as $$
begin
return;
END;
$$ language plpgsql;
CREATE FUNCTION

The parameters are irrelevant, but:

  • You need > 1 of them
  • They have to be named

And now you can:

# select * from x();
 o1 | o2
----+----
(0 rows)
answered 2009-08-28T06:33:29.603

Your Answer