如何在PostgreSQL中消除多个INOUT参数的函数调用

时间:2016-03-14 18:38:44

标签: postgresql inout

我将代码从Oracle移植到PosgreSQL(经典,Carl)。 我发现,我的函数调用了3次,我做了这个简单的例子: 请看我的例子:

create function trak_test001(inout int,inout int, inout int)
AS
$BODY$
<<trak_test001>>
DECLARE
  x ALIAS FOR $1;
  y ALIAS FOR $2;
  z ALIAS FOR $3;

BEGIN
  Raise DEBUG  'trak_test001 FOR x=% y=% z=% ',x,y,z;
  x:=x+1;
  y:=y+1;
  z:=z+1;
END trak_test001;
$BODY$
LANGUAGE plpgsql VOLATILE 
COST 1000;

create function trak_test002()
RETURNS void
AS
$BODY$
<<trak_test002>>
DECLARE
  x int default 0;
  y int default 0;
  z int default 0;

BEGIN
  Raise DEBUG  'trak_test002 FOR x=% y=% z=% ',x,y,z;
 SELECT (trak_test001(x,y,z)).* INTO x,y,z;
    Raise DEBUG  'trak_test002 FOR x=% y=% z=% ',x,y,z;
END trak_test002;
$BODY$
LANGUAGE plpgsql VOLATILE 
COST 1000;

SELECT trak_test002();

得到下一个调试输出:

DEBUG:  trak_test002 FOR x=0 y=0 z=0
DEBUG:  trak_test001 FOR x=0 y=0 z=0 
CONTEXT:  SQL statement "SELECT (trak_test001(x,y,z)).*"
PL/pgSQL function trak_test002() line 10 at SQL statement
DEBUG:  trak_test001 FOR x=0 y=0 z=0 
CONTEXT:  SQL statement "SELECT (trak_test001(x,y,z)).*"
PL/pgSQL function trak_test002() line 10 at SQL statement
DEBUG:  trak_test001 FOR x=0 y=0 z=0 
CONTEXT:  SQL statement "SELECT (trak_test001(x,y,z)).*"
PL/pgSQL function trak_test002() line 10 at SQL statement
DEBUG:  trak_test002 FOR x=1 y=1 z=1
Total query runtime: 56 msec

所以这是个问题:为什么我的函数调用3次以及如何避免这种情况? 谢谢。

1 个答案:

答案 0 :(得分:0)

我是个白痴。使用多个INOUT参数调用函数的正确方法是:

create function trak_test002()
RETURNS void
AS
$BODY$
<<trak_test002>>
DECLARE
  x int default 0;
  y int default 0;
  z int default 0;

BEGIN
  Raise DEBUG  'trak_test002 FOR x=% y=% z=% ',x,y,z;
 SELECT * INTO x,y,z FROM trak_test001(x,y,z);
    Raise DEBUG  'trak_test002 FOR x=% y=% z=% ',x,y,z;
END trak_test002;
$BODY$
LANGUAGE plpgsql VOLATILE 
COST 1000;