lightdb plorasql集合类型新增可变数组

文章目录

背景

在信创适配中,从Oracle迁移过来的存储过程使用到可变数组。因此在LightDB-X 23.4版本中对现有的集合类型进行了增强,添加了可变数组类型。

集合类型

在LightDB-X 23.4版本开始plorasql支持的集合类型同Oracle一致,覆盖了关联数组,可变数组和嵌套表三种类型,支持的方法如下

  • DELETE 从集合中删除元素。
  • EXTEND 将元素添加到集合的末尾。
  • EXISTS 如果集合的指定元素存在,则返回TRUE。
  • FIRST 返回集合中的第一个索引。
  • LAST 返回集合中的最后一个索引。
  • COUNT 返回集合中的元素数量。

可变数组

可变大小数组是一个数组,其元素数量可以从零(空)到声明的最大大小不等,在这里超过也支持。

要访问可变数组的元素,使用语法variable_name(index)。在数据库中存储和检索可变数组时,其索引和元素顺序将保持稳定。

随着您添加或删除元素,可变数组变量占用的内存量可以动态增加或减少。

目前只支持本地类型的可变数组,不支持全局可变数组。

可变数组示例

准备数据

复制代码
select dbms_output.serveroutput(true);
create table user_info(id int, name varchar(30));
insert into user_info values( 11, 'zhangsan');
insert into user_info values( 21, 'lisi');
insert into user_info values( 31, 'wangwu');

匿名块

复制代码
declare
    type id_array is varray(10) of INTEGER;
    ids id_array := id_array(11, 21);
    v_name varchar2(30);
begin
    if ids.exists(3) then
        dbms_output.put_line('ids(3) exists.');
    else
        dbms_output.put_line('ids(3) not exists.');
    end if;
    
    ids.extend;
    ids(ids.count) := 31;
    
    if ids.exists(3) then
        dbms_output.put_line('ids(3) exists.');
    else
        dbms_output.put_line('ids(3) not exists.');
    end if;
    
    dbms_output.put_line('count=' || ids.count);
    
    for i in ids.first .. ids.last loop
        select name into v_name from user_info where id = ids(i);
        dbms_output.put_line('ids(' || i || ') name is ' || v_name);
    end loop;

    ids.delete;
    dbms_output.put_line('after is.delete, count=' || ids.count);
end;
/

ids(3) not exists.
ids(3) exists.
count=3
ids(1) name is zhangsan
ids(2) name is lisi
ids(3) name is wangwu
after is.delete, count=0
DO

函数

复制代码
create or replace function f_varray() return int is
    type id_array is varray(10) of INTEGER;
    ids id_array := id_array(11, 21);
    v_name varchar2(30);
begin
    if ids.exists(3) then
        dbms_output.put_line('ids(3) exists.');
    else
        dbms_output.put_line('ids(3) not exists.');
    end if;
    
    ids.extend;
    ids(ids.count) := 31;
    
    if ids.exists(3) then
        dbms_output.put_line('ids(3) exists.');
    else
        dbms_output.put_line('ids(3) not exists.');
    end if;
    
    dbms_output.put_line('count=' || ids.count);
    
    for i in ids.first .. ids.last loop
        select name into v_name from user_info where id = ids(i);
        dbms_output.put_line('ids(' || i || ') name is ' || v_name);
    end loop;

    ids.delete;
    dbms_output.put_line('after is.delete, count=' || ids.count);
    return ids.count;
end;
/

select f_varray();

ids(3) not exists.
ids(3) exists.
count=3
ids(1) name is zhangsan
ids(2) name is lisi
ids(3) name is wangwu
after is.delete, count=0
 f_varray 
----------
        0
(1 row)

存储过程

复制代码
create or replace procedure p_varray() is
    type id_array is varray(10) of INTEGER;
    ids id_array := id_array(11, 21);
    v_name varchar2(30);
begin
    if ids.exists(3) then
        dbms_output.put_line('ids(3) exists.');
    else
        dbms_output.put_line('ids(3) not exists.');
    end if;
    
    ids.extend;
    ids(ids.count) := 31;
    
    if ids.exists(3) then
        dbms_output.put_line('ids(3) exists.');
    else
        dbms_output.put_line('ids(3) not exists.');
    end if;
    
    dbms_output.put_line('count=' || ids.count);
    
    for i in ids.first .. ids.last loop
        select name into v_name from user_info where id = ids(i);
        dbms_output.put_line('ids(' || i || ') name is ' || v_name);
    end loop;

    ids.delete;
    dbms_output.put_line('after is.delete, count=' || ids.count);
end;
/

call p_varray();

ids(3) not exists.
ids(3) exists.
count=3
ids(1) name is zhangsan
ids(2) name is lisi
ids(3) name is wangwu
after is.delete, count=0
CALL
相关推荐
这个DBA有点耶2 天前
MVCC深入:Read View、版本链与快照读——InnoDB并发控制的内核
数据库·mysql·架构
DBA_G2 天前
从地面到云霄:GBase数据库在民航三大场景的落地实践
数据库
自由能燃气设备2 天前
商用全预混低氮冷凝锅炉免费方案vs付费方案对比+选型避坑指南
大数据·数据库·人工智能
科创致远2 天前
科创致远 ESOP 系统核心效能与实战价值展示
大数据·数据库·人工智能·精益工程
2601_962218612 天前
万象生鲜系统称重自动多退少补算法解决生鲜非标品痛点
大数据·数据库·人工智能·python·算法
张洛闻Eren2 天前
k8s云原生【第十课】:水平 Pod 自动扩缩容
运维·数据库·云原生·kubernetes·github
于平安2 天前
MySQL-触发器
数据库·mysql
白远山2 天前
上海24小时自助健身房解决方案实战指南与经验分享
java·数据库·架构·需求分析
Omics Pro2 天前
斯坦福Nature+Science|广义虚拟细胞基础大模型
数据库·人工智能·算法·机器学习·自然语言处理
2603_965148112 天前
AI+API选品:下一代智能商务助手雏形已现
java·大数据·数据库·人工智能·数据挖掘