KingbaseES OUT 类型参数过程与函数的调用方法

对于含有 out 类型参数的过程或者函数,只能通过块方式调用,这是因为,ksql 还不支持类似 Oracle 那样通过 var 定义变量。

一、带OUT的procedure 调用

创建过程:

create or replace procedure proc1( v_id integer, out v_retcode text, out v_retinfo text, out v_row_num integer)
AS
declare
begin
   insert into table_new(id, name, age) select t.id, t.name, m.age from student t, employees m where t.id=m.id and t.id=v_id;
   GET DIAGNOSTICS V_ROW_NUM := ROW_COUNT;
   
   -- 执行成功后的返回信息
   V_RETCODE := 'SUCCESS';
   V_RETINFO := '结束';
   
   --异常处理
   EXCEPTION
   WHEN OTHERS THEN
       V_RETCODE := 'FAIL';
       V_RETINFO := SQLERRM;
end;
/

调用过程:

declare
  v_retcode text;
  v_retinfo text;
  v_row_num integer;
  v_id integer;
begin
  v_id:=30;
  CALL proc1(v_id, v_retcode, v_retinfo, v_row_num);
  raise notice 'proc1 result is:  %, %, %', v_retcode, v_retinfo, v_row_num;
end;
/

二、带 OUT 参数的function调用

创建函数:

create or replace function fun1( v_id integer, out v_retcode text, out v_retinfo text, out v_row_num integer)
AS $$
declare
begin
   insert into table_new(id, name, age) select t.id, t.name, m.age from student t, employees m where t.id=m.id and t.id=v_id;
   GET DIAGNOSTICS V_ROW_NUM := ROW_COUNT;
   
   -- 执行成功后的返回信息
   V_RETCODE := 'SUCCESS';
   V_RETINFO := '结束';
   
   --异常处理
   EXCEPTION
   WHEN OTHERS THEN
       V_RETCODE := 'FAIL';
       V_RETINFO := SQLERRM;
end;
$$ language plpgsql
/

调用函数:

--function 可以不需要在 block 里调用
select fun1(12);


--但如果需要out返回信息,则必须通过block 传入变量
declare
  v_retcode text;
  v_retinfo text;
  v_row_num integer;
  v_id integer;
begin
  v_id:=30;
  select * from  fun1(v_id) into v_retcode, v_retinfo, v_row_num;
  raise notice 'proc1 result is:  %, %, %', v_retcode, v_retinfo, v_row_num;
end;
/

 

posted @ 2021-07-28 20:00  KINGBASE研究院  阅读(342)  评论(0编辑  收藏  举报