我如何使用或正确地 return 来自我的 PostgreSQL 函数的值?

How Do I use or properly return a value from my PostgreSQL function?

CREATE OR REPLACE FUNCTION data.first()
AS $BODY$
DECLARE
  res numeric;
BEGIN
  PERFORM data.second(5,3,4);
  IF(res > 10)THEN
    something
  ELSEIF(res < 10)THEN
    something else
  END IF
END;
$BODY$;

=========================================

CREATE OR REPLACE FUNCTION data.second(
a numeric,
b numeric,
c numeric
OUT res numeric
)
RETURNS numeric
AS $BODY$
BEGIN
  res = a + b;
END;
$BODY$;

如何在父函数中使用 res?

不要同时指定 OUT 和函数 returns:

t=# CREATE OR REPLACE FUNCTION data.second(
a numeric,
b numeric,
c numeric,
OUT res numeric
)
AS $BODY$
DECLARE
BEGIN
res = a + b;
END;
$BODY$ language plpgsql;
CREATE FUNCTION

如果你想使用函数的return,使用select into VAR,执行将只执行函数并丢弃它的输出:

t=# CREATE OR REPLACE FUNCTION data.first() returns text
AS $BODY$
DECLARE
res numeric;
BEGIN
SELECT data.second(5,3,4) INTO res;
IF(res > 5)THEN
  raise info 'second returned %',res;
END IF;
RETURN 'here is the return';
END;
$BODY$ language plpgsql;
CREATE FUNCTION

最后:

t=# select * from data.first();
INFO:  second returned 8
       first
--------------------
 here is the return
(1 row)

https://www.postgresql.org/docs/current/static/plpgsql.html