摘要:本文主要讲解DWS函数出参带出方式。
本文分享自华为云社区《GaussDB(DWS)功能 -- 函数出参 #【玩转PB级数仓GaussDB(DWS)】》,作者:譡里个檔 。
DWS的PL/pgSQL函数/存储过程中有一个特殊的语法PERFORM语法,用于执行语句但是丢弃执行结果的场景,常用于一些状态判断的场景。但是客户往往会不当使用PERFORM语法,导致业务逻辑出错,最常见的就是使用PERFORM执行带有出参的函数。
已知函数inner定义如下
CREATE OR REPLACE FUNCTION public.inner( IN a1 integer, IN b1 integer, OUT a integer, OUT b integer ) RETURNS record LANGUAGE plpgsql NOT FENCED NOT SHIPPABLE AS $function$ DECLARE BEGIN a := a1; b := b1; END$function$ ;
函数f_outer定义如下,函数体中调用函数inner,把函数的出参赋值给变量a, b
CREATE OR REPLACE FUNCTION public.f_outer(IN i_a int, IN i_b int) RETURNS void LANGUAGE plpgsql NOT FENCED NOT SHIPPABLE AS $function$ DECLARE a int; b int; BEGIN PERFORM public.inner(i_a, i_b, a, b); RAISE INFO 'a = %, b = %', a, b; END$function$ ;
但是实际执行的时候发现函数inner的出参没有正确赋值(预期值为a = 1 b = 11)。
postgres=# CALL f_outer(1, 11); INFO: a = <NULL>, b = <NULL> SQLSTATE: 00000 f_outer --------- (1 row) Time: 1.086 ms
出现这种问题的原因是PERFORM语法会执行SQL语句,但是会抛弃执行结果,导致函数出参没有赋值
DWS中常用的带出函数出参的方式有以下三种:
CREATE OR REPLACE FUNCTION public.test1(IN i_a int, IN i_b int) RETURNS void LANGUAGE plpgsql NOT FENCED NOT SHIPPABLE AS $function$ DECLARE a int; b int; BEGIN public.inner(i_a, i_b, a, b); RAISE info 'a = %, b = %', a, b; END$function$ ;
CREATE OR REPLACE FUNCTION public.test2(IN i_a int, IN i_b int) RETURNS void LANGUAGE plpgsql NOT FENCED NOT SHIPPABLE AS $function$ DECLARE a int; b int; BEGIN EXECUTE IMMEDIATE 'SELECT * from public.inner(:1, :2)' UNSING INTO a, b USING IN i_a, i_b; RAISE INFO 'a = %, b = %', a, b; END$function$ ;
CREATE OR REPLACE FUNCTION public.test3(IN i_a int, IN i_b int) RETURNS void LANGUAGE plpgsql NOT FENCED NOT SHIPPABLE AS $function$ DECLARE a int := 0; b int := 0; BEGIN SELECT * INTO a, b FROM public.inner(i_a, i_b); RAISE INFO 'a = %, b = %', a, b; END$function$ ;