PostgreSQL:在plpgsql函数中访问RECORD类型的数据

时间:2016-05-17 11:32:08

标签: postgresql function plpgsql

这是我的第一个功能:

CREATE  FUNCTION func1(INOUT a  integer,OUT b integer,  OUT c float, OUT   d integer,OUT e decimal)
 AS  $BODY$
DECLARE

BEGIN
.
.
.   
END
$BODY$
LANGUAGE plpgsql VOLATILE

输出是:

  

$ SELECT * FROM func1(242);

     

a | b | c | d | Ë
   - + - + - + - + ---

     

242 | 5 | 7.5 | 15 | 1.2208

这是第二个功能:

CREATE  FUNCTION func2()
   AS  $BODY$
DECLARE
   d  RECORD;

BEGIN
    EXECUTE 'SELECT * FROM func1(242)' INTO d;  
    raise notice 'arr: %', d;.  -– output is->  NOTICE:  arr: (242,5,7.5,15,1.2208)
--HERE I want access to a,b,....
    .
    .   
END
$BODY$
LANGUAGE plpgsql VOLATILE

如何在func2中单独访问a,b,c,d和e变量。

1 个答案:

答案 0 :(得分:1)

这是一个解决方案:

FOR d in EXECUTE 'SELECT * FROM func1(242)'
LOOP
  --here you can access them, or assign them to a local variable
  raise notice 'a:%, b:%, c:%, d:%, e:%', d.a, d.b, d.c, d.d, d.d, d.e;
END LOOP;