GBase 8s LIST类型

1,定义 row type

CREATE ROW TYPE rowtype_list(id int, name varchar(40));

2,创建 spl,返回LIST类型

-- 初始化list,并返回
DROP FUNCTION IF EXISTS get_list;
CREATE FUNCTION get_list()
  RETURNING LIST(rowtype_list not null)
  
  DEFINE v_list LIST(rowtype_list not null);
  DEFINE i int;
  
  LET v_list = list{};
  
  FOR i = 1 TO 10
    INSERT INTO TABLE(v_list) values(ROW(i,'test'||i)::rowtype_list);
  END FOR;
  
  RETURN v_list;
END FUNCTION;

3,传入LIST参数,使用游标处理。

-- LIST当作参数输入
DROP FUNCTION IF EXISTS call_list;
CREATE FUNCTION call_list(p_list LIST(rowtype_list not null)) RETURNING varchar(40);
  DEFINE v_id int;
  DEFINE v_name varchar(40);
  
  FOREACH cur1 FOR SELECT id, name INTO v_id,v_name FROM TABLE(p_list)
    if mod(v_id,3) == 1 then
      RETURN v_name WITH RESUME;
    end if;
  END FOREACH;
END FUNCTION;

4,调用

call call_list(get_list());

结果:

(expression)

test1
test4
test7
test10

标签: gbase 8s, list, row type

评论已关闭