记录 在 pl/sql 中可以定义三种类型的记录 : 显式定义的记录,面向数据库表的记录,面向游标的记录。
记录的定义其语法如下: Type record_type_name is record( Field_name1 datatype not null default )
例子: Type person_rec is record ( Name varchar2(100); Address varchar2(200); ); 记录类型变量的声名 vperson person_rec;
type …record 只是一种操象的定义,因此,它不能由它自己使用, type.. record 不会占用任何资源,直到专名该类型的变量时才占用资源
记录类型的使用 记录类型的变量定义以后,接下来就是使用记录来处理数据。 1 、访问记录中的远素 2 、测试记录是否相等 记录比较应比较完记录的每个字段 , 看它们是否相等,把记录看作整体来比较的做的作法是错误的。 记录的使用就像一个对象。可以对它进行赋值, oracle 9i 中有几种赋值方法: 1) 对各个字段赋值 2) 使用 select into 语句填充一个记录 3) 使用 fetch into 语句填充一个记录 4) 使用一个记录为另一个记录赋值(聚集赋值)
其它记类型 1 面向表的记录类型 定义 : Record_var_name table_name%rowtype; 使用: Select * into record_var_name from hrc_tab where id=? 2 面向游标的记录类型 Declare Cursor csr_hrc is select * from hrc_tab order by 1; hrc_rec csr_hrc%rowtype; begin …….loop Fetch csr_hrc into hrc_rec Exit when csr_hrc%not found ….. End loop; end; 涉及整个 pl/sql 记录的 DML 操作(特指 insert ,update 和 select ) Insert into hrc_tab values record_var_name
Update hrc_tab set ROW=record_var_name where hrc_code=’
Index-by
表
Index-by 表是一同一个与数组类似的同质元素的集合组成的一种复合数据类型。集合中的元素稀 分布且没有限定边界,只是由一个整数索引将其连接在一起。
定义 Index-by 表的型 Type table_name_type is table of type [not null] index by binary_integer 下面是一个声明这种类型的示例 Type num_tab is table of number index by binary_integer 声明表类型的变量 V_num_tab num_tab;
Index-by
表的使用
index-by 表是两列的(一列索引,另一列是对应的值)结构, index-by 是 pl/sql 特有,所以在 sql 中它们自然就不可用,另外,也不能存储在数据库中,这样导致不能在 index-by 表中使用 select ,insert ,update 和 delete 等 SQL DML 操作。对 index-by 表来说没有 commit 或 rollback ,一个例子: declare type num_tab is table of number index by binary_integer; v_example_tab num_tab; v_num number:=13; begin v_example_tab(1):=1001; v_example_tab (0):=1002; v_example_Tab(-10):=1003 v_example_tab(v_num):=1004p; dbms_output.put_line(to_char(v_example_tab(-10))|| …..) end;
可以使用任何与 binary_integer 相兼容的文字,变量或表达式作为 index-by 表的索引
可样也可以把一个 index-by 表赋值给另一个 index-by 表。 declare type num_tab is table of number index by binary_integer; v_example_tab1 num_tab; v_example_tab2 num_tab; begin v_example_tab1:= v_example_tab2; end;
在 index-by 表中只有在赋值时才可以创建行,因些在赋值以前,行是一存在的,这是因为 index-by 表没有上下界,访问没定义的行会出现 N)_DATA_FOUND 行。
Index-by 表的填充有三种方法: 1 通过赋值创建元素行, 2 使用 loop 为 index-by 表的行赋值 3 用一个 index-by 表为另一个 index-by 表赋值 第一种方法前面用了,第二种方法就是一个循环了,如 : Declare Type num_tab is table of number index by binary_integer; V_Example_tab num_tab; Begin For idx in 1..10 loop V_example_tab(idx) :=(2*idx)+1 End loop; End;
删除 Index-by 表 有两种方法: 1、 定义一下与已填充的 index-by 表类型相同的空的 index-by 表,然后用该表执行聚集赋值 2、 使用 index-by 表的 delete 方法。 Index-by 表的方法:
方法使用例子: If v_example.exists(100) then dbms_output.put_line(to_char(v_example.exists(100))); End if ;
For udx in 1..v_example.count loop // 用这种方法必须要确定行是连续填充的,下界是一个 // 定的值。 Dbms_output.put_line(to_char(v_example_tab(idx))); End loop;
V_example_tab.delete(1) ; // 删除第一个索引的记录 V_example_tab.delete(2,5);// 删除第 2 至第 5 个索引记录 V_example_tab.delete;// 删除所有记录
使用 first,last,next 方法的例子 Idx:=v_example_tab.first;
Dbms_output.put_line(); Exit when idx:=v_example_tab.last; Idx:=v_example_tab.next(); End loop;
这样遍历一个 index-by 表是最好的,表的记录可以不连续。
记录的
Index-by
表
定义 index-by 表的几个步骤
1、 定义记录类型 2、 定义记录类型的表类型 3、 定义表类型的变量
一个例子: Declare Type hrc_org_Rdc is record( Hrc_org_id number, Hrc_descr var_char2(200) ) Type hrc_org_tab is table of hrc_org_rec index by binary_integer; Begin Null; End;
联合数组
在 oracle 9i release 2 中,可以使用 varchar2 类型的索引代规 binary_integer 类型的索引来定义 index-by 表,用 varchar2 索引作为数据库表的关键字,可以提高数据库的性能。 例子: Declare Type site_ary is table of number index by varchar2(20); V_example_array site_ary; Begin V_example_array(‘xx’):=10; V_example_array(‘yy’):=200; End;
|
|