oracle 数组实现

来源:互联网 发布:现在usb属于什么端口 编辑:程序博客网 时间:2024/06/05 11:09

-一维数组:
--嵌套表
--尺寸没有限制。
--本质上是无序的
--VARRAY
--尺寸必须固定,所有的实例尺寸相同。
--在过程化语言中可以作为有序数组进行检索但在Oracle内部看成单个不能分割的单元。
--存储效率高。

--多维数组
--利用record 和record of


--建立测试
drop table t_test_1;
create table t_test_1(
pid    number(10),
pname  varchar2(20),
birth  date,
score  number(3),
note   varchar2(50)
);

--初始化,插入数据
--利用了nested table 来实现一维数组
--不需要制定上下限,下表可以不连续,需初始化
--
declare
  type type_test_pid is table of t_test_1.pid%type
  index by binary_integer;
  type type_test_pname is table of t_test_1.pname%type
  index by binary_integer;
  type type_test_birth is table of t_test_1.birth%type
  index by binary_integer;
  type type_test_score is table of t_test_1.score%type
  index by binary_integer;
  type type_test_note is table of t_test_1.note%type
  index by binary_integer;
 
  my_test_pid   type_test_pid;
  my_test_panme type_test_pname;
  my_test_birth type_test_birth;
  my_test_score type_test_score;
  my_test_note  type_test_note;

  i number(10) := 0;

begin
  for i in 1 .. 10000
  loop
    my_test_pid(i) := i;
    my_test_panme(i) := 'names_' || to_char(i);
    my_test_birth(i) :=  sysdate;
    my_test_score(i) := to_number(nvl(substr(to_char(i), -2), to_char(i)));
    my_test_note(i) := my_test_panme(i)|| ' score is ' ||
                    nvl(substr(to_char(i), -2), to_char(i));                   
  end loop;

  forall i in 1 .. 10000
    insert into t_test_1
    values(
    my_test_pid(i),
    my_test_panme(i),
    my_test_birth(i),
    my_test_score(i),
    my_test_note(i)
    );
 
  commit;
  exception
  when others then
       rollback;
end;

--使用varray实现数组
--自定义一个TYPE使用VARRAY来得到一个数组
--但只能对基本类型定义
--此种方式实现的数组,需要制定上限,且下标连续,在使用之前须初始化
--适用于数组长度不太大的情况

declare
type num_pid is varray(1000) of t_test_1.pid%type;
v_num_pid    num_pid;
i number(8):=1;
begin
v_num_pid := num_pid();
v_num_pid.extend;
v_num_pid(1) := 1;
v_num_pid.EXTEND(999,1);--一次性添加999个第1个元素的副本
for i in 1 .. 1000
loop
--v_num_pid.extend; --每次扩展一个
v_num_pid(i) := i;
execute immediate 'update t_test_1 set score = trunc(score/2)
where pid=:1 '
using v_num_pid(i);
end loop;
end;

-使用record和table实现多维数组

 

Oracle系列:RecordPL/SQL(table)

一,什么是记录RecordPL/SQL表?

记录Record:由单行多列的标量类型构成的临时记录对象类型。类似于多维数组。

PL/SQL表:由多行单列的索引列和可用列构成的临时索引表对象类型。类似于一维数组和键值对。

都是用户自定义数据类型。

二,Record + PL/SQL 用途是什么?

Record + PL/SQL表可以进行数据的多行多列存储。这样我们就可使用Record + PL/SQL表在需要时封装一个临时的表对象,进行传递和操作。

通过Record自定义表结构,封装一条记录。PL/SQL表声明 可用列 类型 Record类型(将可用列指向Record类型变量),每个索引对应一个Record类型变量。

三,使用Record + PL/SQL表进行数据的多行多列存储

①声明Record类型和PL/SQL表,

其中PL/SQL表的索引列为主键约束和唯一约束列或自增Integer。可用列为Record类型或%RowType类型。

②填充PL/SQL表可用列(Record类型):通过索引指向Record,使用Record访问记录成员。

   语法:

    PL/SQL表名(索引列值).记录成员 := 记录成员类型值;

    

    PL/SQL表名(索引列值) := Record类型变量;

    --注意其PL/SQL表中声明的可用列要和这里赋值的Record类型变量结构一样

③访问PL/SQL

下面是例子:

/*conn scott/tiger

   Create table empa as select * from emp;

*/

例子:

Declare

   Type RecType Is Record

   (

  rno empa.empno%type,

    rname empa.ename%type,

    rsal empa.sal%type

   );

   Type TabType Is Table Of RecType Index By Binary_Integer;

   MyTab TabType;

   vN Number;

Begin

   --填充

   vN := 1;

   For varR In (Select * From empa Order By empno ASC) Loop

    MyTab(vN).rno := varR.empno;

    MyTab(vN).rname := varR.ename;

    MyTab(vN).rsal := varR.sal;

    vN := vN + 1;

   End Loop;

   --访问

   vN := MyTab.First;

--取得表记录的第一个下标,不一定是1哦,让然在这里确实是1,因为上面是从1开始存的哦

   For varR In vN..MyTab.count Loop

    DBMS_OUTPUT.PUT_LINE(vN ||'   '||MyTab(vN).rno||'   '||MyTab(vN).rname||'   '||MyTab(vN).rsal);

    vN := MyTab.Next(vN);--可以换成vn:=vn+1;

   End Loop;

End;

例子:利用记录Record可用整体赋值的特性来填充PL/SQL

Declare

   Type RecType Is Record

   (

    rno empa.empno%type,

    rname empa.ename%type,

    rsal empa.sal%type

   );

   Type TabType Is Table Of RecType Index By Binary_Integer;

   MyTab TabType;

   vN Number;

Begin

   --填充

   vN := 1;

   For varR In (Select empno, ename, sal From empa Order By empno ASC)

Loop

      MyTab(vN) := varR; --记录整体赋值

       vN := vN + 1;

   End Loop;

   --访问

   vN := MyTab.First;

   For varR In vN..MyTab.count

   Loop

    DBMS_OUTPUT.PUT_LINE(vN ||'   '||MyTab(vN).rno||'   '||MyTab(vN).rname||'   '||MyTab(vN).rsal);

    vN := MyTab.Next(vN);

   End Loop;

End;

例子:使用主键约束和唯一约束列做为索引列(使用EMP表中的empno作为索引列)

   和使用自定义Record类型作为可用列

Declare

   Type RecType Is Record

   (

    rno empa.empno%Type,

    rname empa.ename%Type,

    rsal empa.sal%Type

   );

   Type TabType Is Table Of RecType Index By Binary_Integer;

   MyTab TabType;

   vN empa.empno%Type;

Begin

   --填充

   For varR In (Select empno,ename,sal From empa Order By empno) Loop

MyTab(varR.empno) := varR;

注意这一点与上面的填充是不同的哦,上面的表索引时1,2,3。。。。知道把所有数据都放进去,这里的表索引是emp表的主键哦,这样下面在去这个table中的值时就不可以简简单单的让索引+1了,而是使用next()函数了

   End Loop;

   --访问

   vN := MyTab.First;

   For varR In 1..MyTab.Count

   Loop

    DBMS_OUTPUT.PUT_LINE(MyTab(vN).rno||'   '||MyTab(vN).rname||'   '||MyTab(vN).rsal);

   vN := MyTab.Next(vN);

   End Loop;

End;

例子:使用主键约束和唯一约束列做为索引列(使用EMP表中的empno作为索引列)

     和使用%RowType类型作为可用列

Declare

   Type TabType Is Table Of empa%RowType Index By Binary_Integer;

   MyTab TabType;

   vN empa.empno%Type;

Begin

   --填充

   For varR In (Select * From empa Order By empno)

   Loop

    MyTab(varR.empno) := varR;

   End Loop;

   --访问

   vN := MyTab.First;

   For varR In 1..MyTab.Count

   Loop

    DBMS_OUTPUT.PUT_LINE(MyTab(vN).empno||'   '||MyTab(vN).ename||'   '||MyTab(vN).sal);

    vN := MyTab.Next(vN);

   End Loop;

End;

Select   version   FROM   Product_component_version   

Where   SUBSTR(PRODUCT,1,6)='Oracle';

原创粉丝点击