Postgresql,一个函数调用另一个函数并向调用者获取 2 个输出值
Postgresql, one function calls another function and get 2 out values to caller
Postgresql 12. 想从另一个函数 testB() 调用函数 testA(),并将两个“out”值分配给 2 个局部变量。不会做作业,求助
create or replace function testA(
out outA int,
out outB varchar)
returns record as $$
begin
outA := 100;
outB := '1234';
end; $$ language plpgsql;
create or replace function testB()
returns void as $$
declare aa int; bb varchar;
begin
-- select * from testA(aa, bb);
end; $$ language plpgsql;
像这样:
CREATE OR REPLACE FUNCTION public.testa(OUT outa integer, OUT outb character varying)
RETURNS record
LANGUAGE plpgsql
AS $function$
begin
outA := 100;
outB := '1234';
end; $function$
;
CREATE OR REPLACE FUNCTION public.testb()
RETURNS void
LANGUAGE plpgsql
AS $function$
declare
aa int; bb varchar;
begin
select into aa, bb * from testa();
RAISE NOTICE 'a: %, b: %', aa, bb;
end; $function$
;
select * from testb();
NOTICE: a: 100, b: 1234
testb
-------
(1 row)
SELECT INTO
会将testa的输出赋值给你在testb中声明的变量。有关详细信息,请参阅 here。
Postgresql 12. 想从另一个函数 testB() 调用函数 testA(),并将两个“out”值分配给 2 个局部变量。不会做作业,求助
create or replace function testA(
out outA int,
out outB varchar)
returns record as $$
begin
outA := 100;
outB := '1234';
end; $$ language plpgsql;
create or replace function testB()
returns void as $$
declare aa int; bb varchar;
begin
-- select * from testA(aa, bb);
end; $$ language plpgsql;
像这样:
CREATE OR REPLACE FUNCTION public.testa(OUT outa integer, OUT outb character varying)
RETURNS record
LANGUAGE plpgsql
AS $function$
begin
outA := 100;
outB := '1234';
end; $function$
;
CREATE OR REPLACE FUNCTION public.testb()
RETURNS void
LANGUAGE plpgsql
AS $function$
declare
aa int; bb varchar;
begin
select into aa, bb * from testa();
RAISE NOTICE 'a: %, b: %', aa, bb;
end; $function$
;
select * from testb();
NOTICE: a: 100, b: 1234
testb
-------
(1 row)
SELECT INTO
会将testa的输出赋值给你在testb中声明的变量。有关详细信息,请参阅 here。