I have this in a stored proc in SQL Server:
if not exists
(my select statement)
insert T
(a, b, c)
values
(v_a, v_b, v_c) -- arguments passed to the function
select a,b,c from T where...; -- return the row to the client after inserting it
Does postgreSQL have a counterpart to that if exists ( {select-statement} ) construct? The plpgsql compiler at first told me there was a missing “THEN” but when I corrected the if ... then syntax:
create function foo(v_a int, v_b int, v_c int)
returns TABLE (a int, b int, c int)
as $body$
begin
if not exists
(select id from T where ...) then
insert into T
(a, b, c)
values
(v_a, v_b, v_c);
return QUERY
select a,b,c from T where ... ;
end
$body$
LANGUAGE 'plpgsql'
the compilation terminates with:
ERROR: syntax error at end of input
LINE 52: $body$ LANGUAGE 'plpgsql'
.........^
so I’m assuming there’s some error upstream, but I don’t see it.
Your
IFclause is incomplete. It must be closed by the correspondingEND IFstatement.Check the docs on conditionals.