plsql-index-by表

5. Index-by表

5.1 index-by表的定义和操作
 
  定义:由与数组类似的同质元素的集合组成的一种复合数据类型
  特点:集合中的元素是稀疏分布的,没有限定的边界,只是由整数作为索引将其连接在一起,索引可以是正、负整数或者0

(1)定义和赋值
 
  A 定义数字类型的index-by表的类型

declare
  type num_table is table of number index by binary_integer;      --binary_integer:整数
  v_example_tab num_table;
  v_num number:=13;
begin
  v_example_tab(1):=1001;
  v_example_tab(2):=1002;
  v_example_tab(10):=1003;
  v_example_tab(-10):=1004;
  v_example_tab(0):=1005;
  v_example_tab(v_num):=1006;
  dbms_output.put_line(to_char(v_example_tab(0)));
end;
 
 B 定义字符串类型的index-by表的类型

declare
  type char_table is table of varchar2(20) index by binary_integer;   --binary_integer:整数
  v_example_tab char_table;
  v_num number:=13;
begin
  v_example_tab(1):=‘xxxx‘;
  v_example_tab(2):=‘yyhy‘;
  v_example_tab(10):=‘asdf‘;
  v_example_tab(-10):=‘asdfasd‘;
  v_example_tab(0):=‘asd‘;
  v_example_tab(v_num):=‘aseas1‘;
  dbms_output.put_line(to_char(v_example_tab(v_num)));
end;
 
 C 定义日期类型的index-by表的类型
declare
  type char_table is table of date index by binary_integer;   --binary_integer:整数
  v_example_tab char_table;
  v_change varchar2(30);
begin
  v_example_tab(1):=sysdate;
  v_example_tab(2):=sysdate+1;
  v_change:=to_char(v_example_tab(2),‘yyyy-mm-dd‘);
  dbms_output.put_line(v_change);
end;

D 还可以存储复合的数据类型

declare
  type hrc_org_rec is record(hrc_code number,hrc_descr varchar2(20));
  type num_table is table of hrc_org_rec index by binary_integer;
  v_example_tab num_table;
begin
  v_example_tab(1).hrc_code:=1002;
  v_example_tab(1).hrc_descr:=‘adsfasdg‘;         --赋值方法
  v_example_tab(2).hrc_code:=1003;
  v_example_tab(2).hrc_descr:=‘qweqr‘;
  dbms_output.put_line(to_char(v_example_tab(1).hrc_code));
  dbms_output.put_line(v_example_tab(1).hrc_descr);
end;
 
(2) 访问未定义的行

declare
  type num_table is table of number index by binary_integer;   --binary_integer:整数
  v_example_tab num_table;
  v_num number:=13;
begin
  v_example_tab(1):=1001;
  v_example_tab(2):=1002;
  v_example_tab(10):=1003;
  v_example_tab(-10):=1004;
  v_example_tab(0):=1005;
  v_example_tab(v_num):=1006;
  dbms_output.put_line(to_char(v_example_tab(0)));
  dbms_output.put_line(to_char(v_example_tab(1)));    --报错前的语句不受影响
  dbms_output.put_line(to_char(v_example_tab(5)));    --访问未定义的行会出错,程序终止,报no_data_found的错
  dbms_output.put_line(to_char(v_example_tab(-10)));
end;

(3)通过赋值元素来创建index-by表的行

declare
  type num_table is table of varchar2(20) index by binary_integer;
  v_example_tab num_table;
  v_num number:=13;
begin
  for idx in 1..10 loop             --for loop循环的循环结果可以是一个数字集合
    v_example_tab(idx):=to_char(2*idx+1);
  end loop;
  for idx in 1..10 loop
    dbms_output.put_line(v_example_tab(idx));
  end loop;
end;

(4) 删除index-by表的内容,整个index-by的内容删除掉

declare
  type num_table is table of number index by binary_integer;
  v_example_tab num_table;
  v_example_tab1 num_table;  --未进行初始化,空的index-by表
  v_num number:=13;
begin
  v_example_tab(1):=1001;
  v_example_tab(2):=1002;
  v_example_tab(10):=1003;
  v_example_tab(-10):=1004;
  v_example_tab(0):=1005;
  v_example_tab(v_num):=1006;
  v_example_tab:=v_example_tab1;   --用未初始化的index-by表赋值方式删除
  dbms_output.put_line(to_char(v_example_tab(0)));      --报错:no_data_found
end;

###########################################################################################

5.2 index-by表的相关方法

(1) exists方法 --判断元素是否存在

declare
  type num_table is table of number index by binary_integer;
  v_example_tab num_table;
  v_num number:=13;
begin
  v_example_tab(1):=1001;
  v_example_tab(2):=1002;
  v_example_tab(10):=1003;
  v_example_tab(-10):=1004;
  v_example_tab(0):=1005;
  v_example_tab(v_num):=1006;
  if v_example_tab.exists(-5) then
    dbms_output.put_line(‘YES‘);
  else
    dbms_output.put_line(‘NO‘);
  end if;
end;

(2) count方法 -- 取index-by表元素的个数,就是计算已经定义的index-by的行(索引、下标)的个数
declare
  type num_table is table of number index by binary_integer;
  v_example_tab num_table;
  v_num number:=13;
  v number;
begin
  v_example_tab(1):=1001;
  v_example_tab(2):=1002;
  v_example_tab(10):=1003;
  v_example_tab(-10):=1004;
  v_example_tab(0):=1005;
  v_example_tab(v_num):=1006;
  v:=v_example_tab.count;
  dbms_output.put_line(to_char(v));
end;

(3) delete方法  --  删除元素

declare
  type num_table is table of varchar2(20) index by binary_integer;
  v_example_tab num_table;
  v_num number;
begin
  for idx in 1..10 loop             
    v_example_tab(idx):=to_char(2*idx+1);
  end loop;
  v_num:=v_example_tab.count;
  dbms_output.put_line(to_char(v_num));
  v_example_tab.delete(1);          --删除index-by表中的对应的索引的值
  v_example_tab.delete(5);
  v_example_tab.delete(7);
  v_num:=v_example_tab.count;
  dbms_output.put_line(to_char(v_num));
end;

结合exists方法使用输出index-by表中的元素

declare
  type num_table is table of varchar2(20) index by binary_integer;
  v_example_tab num_table;
  v_num number;
begin
  for idx in 1..10 loop             
    v_example_tab(idx):=to_char(2*idx+1);
  end loop;
  v_num:=v_example_tab.count;
  dbms_output.put_line(to_char(v_num));
  v_example_tab.delete(1);
  v_example_tab.delete(5);
  v_example_tab.delete(7);
  v_num:=v_example_tab.count;
  dbms_output.put_line(to_char(v_num));
  for idx in 1..10 loop
    if v_example_tab.exists(idx) then             
      dbms_output.put_line(to_char(v_example_tab(idx)));
    else
      dbms_output.put_line(‘NULL‘);
    end if;
  end loop;
end;

(4) first  last  next  prior方法    --取的都是索引值

first方法:取index-by的第一个有值的元素(包含null值)的索引号。
last方法: 取index-by的最后一个有值的元素(包含null值)的索引号。
next方法:取index-by指定元素的下一个有值的元素(包含null值)的索引号
prior方法:取index-by指定元素的上一个有值的元素(包含null值)的索引号

案例:
declare
  type num_table is table of varchar2(20) index by binary_integer;
  v_example_tab num_table;
  v_num number;
  idx number;
begin
  for idx in 1..10 loop             
    v_example_tab(idx):=to_char(2*idx+1);
  end loop;
  v_num:=v_example_tab.count;
  dbms_output.put_line(to_char(v_num));
  v_example_tab.delete(1);
  v_example_tab.delete(5);
  v_example_tab.delete(7);
  v_num:=v_example_tab.count;
  dbms_output.put_line(to_char(v_num));
  idx:=v_example_tab.first;
  loop
    if v_example_tab.exists(idx) then
      dbms_output.put_line(v_example_tab(idx));
    end if;
    exit when idx=v_example_tab.last;   --退出循环的条件满足idx等于最后一个不为空的元素的索引值
    idx:=v_example_tab.next(idx);       --每次循环用next方法将idx往后推,推到下一个不为null的元素的索引位置
  end loop;
end;

(3) index-by表的赋值
 
 引用记录的index-by表将一个记录类型作为存储的类型存在index-by表中

A 分散赋值

declare
  type hrc_org_rec is record(hrc_org_id number,hrc_descr varchar2(20),org_short_name varchar2(30));
  type num_table is table of hrc_org_rec index by binary_integer;
  v_example_table num_table;
  cursor csr_hrc_org is select hrc_org_seq.nextval hrc_org_id,h.hrc_descr,o.org_short_name
                          from org_tab o,hrc_tab h
                         where o.hrc_code=h.hrc_code;
  i integer:=1;
begin
  for idx in csr_hrc_org loop
    v_example_table(i).hrc_org_id:=idx.hrc_org_id;              --分散赋值
    v_example_table(i).hrc_descr:=idx.hrc_descr;
    v_example_table(i).org_short_name:=idx.org_short_name;
    i:=i+1;
  end loop;
  for j in 1..v_example_table.count loop
    if v_example_table.exists(j) then
      dbms_output.put_line(to_char(v_example_table(j).hrc_org_id));  --分别输出
      dbms_output.put_line(v_example_table(j).hrc_descr);
      dbms_output.put_line(v_example_table(j).org_short_name);
    end if;
  end loop;
end;
 
 B 聚集赋值

declare
  type hrc_org_rec is record(hrc_org_id number,hrc_descr varchar2(20),org_short_name varchar2(30));
  type num_table is table of hrc_org_rec index by binary_integer;
  v_example_table num_table;
  cursor csr_hrc_org is select hrc_org_seq.nextval hrc_org_id,h.hrc_descr,o.org_short_name
                          from org_tab o,hrc_tab h
                         where o.hrc_code=h.hrc_code;
  i integer:=1;
begin
  for idx in csr_hrc_org loop
    v_example_table(i):=idx;
    --聚集赋值的方法,将游标句柄赋给index-by,等于把游标字段中每个属性赋给了index-by基于记录的每个字段的属性
    i:=i+1;
  end loop;
  for j in 1..v_example_table.count loop
    if v_example_table.exists(j) then
      dbms_output.put_line(to_char(v_example_table(j).hrc_org_id));
      dbms_output.put_line(v_example_table(j).hrc_descr);
      dbms_output.put_line(v_example_table(j).org_short_name);
    end if;
  end loop;
end;
 
练习:
  创建一个emp_r表(empno number,sal number,hiredate date,dname varchar2(10))
  要求将员工的这四个属性字段存储在记录里,将这些员工的全部信息放置在一个index-by表中,循环的方式将这些取到的信息用记录插入的方式存储在emp_r表中。

时间: 2024-08-26 05:45:57

plsql-index-by表的相关文章

批量修改table和index 的表空间

由于开发人员把ess 项目下的大部分对象放到user 表空间中,用imp/exp 导入正式库后,ess用户的对象还是在users 表空间中.为了把ESS 的对象放到ess 默认的表空间ess中,我按如下几步操作: 1 找出要修改表空间的对象: select * from dba_tables  where owner='ESS' and tablespace_name = 'USERS'; select * from dba_INDEXES where table_owner='ESS' and

PL/SQL基本结构---PLSQL复合类型---表类型变量table

表类型变量table 语法如下:      type 表类型  is table of  类型  index by binary_integer;      表变量名  表类型; 类型可以是前面的类型定义,index by binary_integer 子句代表以符号整数为索引,这样访问表类型变量中的数据方法就是“表变量名(索引符号整数)”.table类型,相当于java中的Map容器,就是一个可变长的数组,key(符号整数索引)必须是整数,可以是负数,value(类型)可以是标量,也可以是re

PLSQL导入Excel表中数据

PL/SQL 和SQL Sever导入excel数据的原理类似,就是找到一个导入excel数据的功能项,按照步骤走就是了.下面是一个些细节过程,希望对像我这样的菜鸟有帮助.  www.2cto.com 1.准备excel表. 右击数据表—选择edit data. 选择数据,右击,选择Copy to Excel 2.调整excel表 可以删除A列和F列,然后把你的数据粘到BCDE列,这样做的好处就是在导入excel的时候,绝对不会出现因为格式或其他文字问题导致错误. 3.准备导入 选择tool—O

Oracle 使用PLSQL 导出 一个表的insert 语句

1. 使用工具 plsql . GUI的方法,如图示 2. 操作界面 3. 然后就看到了插入语句 原文地址:https://www.cnblogs.com/jinanxiaolaohu/p/9192766.html

General PLSQL 查看数据表中文乱码????

新增环境变量即可,具体如下图: 变量名:NLS_LANG 变量值:SIMPLIFIED CHINESE_CHINA.ZHS16GBK

plsql表空间的建立即相关问题

通过pl/sql登录到Oracle数据库上,然后执行菜单:文件/新建/命令窗口 ,打开一个命令窗口然后在该命令窗口中执行脚本创建和删除表空间 创建表空间create tablespace db_name_datafile 'D:\oracle\product\10.2.0\oradata\orcl\db_name_.dbf' size 200Mautoextend on next 10M maxsize unlimited loggingextent management local autoa

PLSQL集合

PLSQL集合 索引表(或者叫做关联数组,associative array ) 嵌套表(nested table) 变长数组(varray) 二维数组(多层集合) 索引表 ---创建索引表类型的语法如下所示: TYPE  type_name IS TABLE OF  element_type INDEX BY index_type; table_name  TYPE_NAME; --其中,element_type 指明集合中存放的数据的类型 --index_type指定下标的类型.只能是整型或

Sybase数据库收集表及其索引的统计信息

更新表及其索引的统计信息: update table statistics 表名 go update index statistics 表名 go 建议此操作在闲时操作.

表视图控制器(TableViewController)(三) 、 表视图搜索

1 乐库的设置界面 1.1 问题 tableView分为静态(static)和动态(dynamic),之前使用的都是动态的tableView,表视图的有多少分区.有多少行以及每一行显示的内容都不是固定的,都由数据模式来决定.而静态的tableView有多少分区.有多少行以及每一行显示的内容都是固定不变的. 静态tableView应用广泛,常用于各种应用软件的设置界面,本案例使用静态tableView完成乐库的设置界面,如图-1所示: 图-1 1.2 方案 由图-1可以看到该界面的显示内容是固定不

mysql key index区别

看似有差不多的作用,加了Key的表与建立了Index的表,都可以进行快速的数据查询.他们之间的区别在于处于不同的层面上. Key即键值,是关系模型理论中的一部份,比如有主键(Primary Key),外键(Foreign Key)等,用于数据完整性检查与唯一性约束等. 而Index则处于实现层面,比如可以对表个的任意列建立索引,那么当建立索引的列处于SQL语句中的Where条件中时,就可以得到快速的数据定 位,从而快速检索.至于Unique Index,则只是属于Index中的一种而已,建立了U