1

我将代码从 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 次以及如何避免这种情况?谢谢你。

4

1 回答 1

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;
于 2016-03-14T18:52:31.447 回答