I want to write a SQL function which returns
true if a certain
INSERT inserted a row and
false if no row was inserted. In this use case, we know that at most one row can be inserted.
I came up with the following solution:
create function test() returns boolean as $$ with dummy as ( insert into ... select ... from ... where ... -- this will limit to at most one row returning true ) select exists(select true from dummy); $$ language sql volatile;
This seems to work fine, but it is rather clumsy and verbose, in my opinion. Is there something better possible?
I first wanted to write the following:
create function test() returns boolean as $$ insert into ... select ... from ... where ... returning true; $$ language sql volatile;
The problem now is that not
null is returned when no row was inserted.
nullfrom a function that returns a boolean.