我在 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
postgreSQL 是否与该if exists ( {select-statement} )
构造对应?plpgsql 编译器起初告诉我缺少“THEN”,但是当我更正if ... then
语法时:
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'
编译以:
ERROR: syntax error at end of input
LINE 52: $body$ LANGUAGE 'plpgsql'
.........^
所以我假设上游有一些错误,但我没有看到。