我写了如下的函数。函数返回记录类型还是什么都不返回?
Create function tmp (in text, in text)
returns setof record as $$
Declare rec record;
Begin
If $1 is not null and $2 is null then
Select * into rec from test_tble where tmprec = $1;
Elsif $1 is not null and $2 is not null then
Insert into test_tble values ($1,$2);
End if;
Return;
End; $$ language plpgsql;
我的问题是第二个条件。 第一个条件很好。 第二个条件成立时。我得到插入记录,但最后返回如下
$ 1 | $ 2
++++++
0行返回。
我不想用此列返回空记录。 任何人都可以帮助我从此恢复。
你期待什么回报?.. –
你可以使用INSERT ... RETURING *来获得插入的行。 - 但是您不必为此使用'plpgsql':它可以通过[数据修改CTE]来实现(https://www.postgresql.org/docs/current/static/queries-with.html#查询,这与修饰)。此外,(有些)新的['UPSERT'('INSERT ... ON冲突不做/更新)](https://www.postgresql.org/docs/current/static/sql-insert.html)功能似乎与你想达到的目标类似。 – pozs
如果第一个条件满足需要返回记录其他条件满足,应该在tmp_tble中插入一条记录并结束 – Harini