您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

oracle数组类型简单实例介绍

2024/6/1 19:21:57发布19次查看
本文章详细的介绍了关于oracle数组的各种操作,有需要的同学可以参考一下。
本文章详细的介绍了关于oracle数组的各种操作,有需要的同学可以参考一下。
oracle数组一般可以分为固定数组和可变数组
 固定数组
代码如下 复制代码
declare 
 type v_ar is varray(10) of varchar2(30);  
 my_ar v_ar:=v_ar('g','m','d','龚','帅');  
 begin 
       for i in 1..my_ar.count 
       loop  
           dbms_output.put_line(my_ar(i));  
       end loop;  
 end; 
 declare
 type v_ar is varray(10) of varchar2(30);
 my_ar v_ar:=v_ar('g','m','d','龚','帅');
 begin
       for i in 1..my_ar.count
       loop
           dbms_output.put_line(my_ar(i));
       end loop;
 end;
--可变数组
 --一维数组
declare 
 type v_table is table of varchar2(30) index by binary_integer;  
 --类型可以是前面的类型定义,index by binary_integer子句代表以符号整数为索引,  
 --这样访问表类型变量中的数据方法就是“表变量名(索引符号整数)”。  
 my_table v_table;  
 begin 
       for i in 1..20  
       loop  
           my_table(i):=i;  
           dbms_output.put_line(my_table(i));  
       end loop;  
 end; 
 declare
 type v_table is table of varchar2(30) index by binary_integer;
 --类型可以是前面的类型定义,index by binary_integer子句代表以符号整数为索引,
 --这样访问表类型变量中的数据方法就是“表变量名(索引符号整数)”。
 my_table v_table;
 begin
       for i in 1..20
       loop
           my_table(i):=i;
           dbms_output.put_line(my_table(i));
       end loop;
 end;
--多维数组--多条记录
declare 
 type v_table is table of t_user%rowtype index by binary_integer;  
 my_table v_table;  
 begin 
       * bulk collect into my_table from t_user;  
       for i in 1..my_table.count/10 --my_table.count/10取到的值为四舍五入值  
       loop  
           dbms_output.put_line('suser--'||my_table(i).suser);  
          dbms_output.put_line('name---'||my_table(i).name);  
           dbms_output.put_line('sex----'||my_table(i).sex);  
       end loop;  
 end; 
 declare
 type v_table is table of t_user%rowtype index by binary_integer;
 my_table v_table;
 begin
       select * bulk collect into my_table from t_user;
       for i in 1..my_table.count/10 --my_table.count/10取到的值为四舍五入值
       loop
           dbms_output.put_line('suser--'||my_table(i).suser);
           dbms_output.put_line('name---'||my_table(i).name);
           dbms_output.put_line('sex----'||my_table(i).sex);
       end loop;
 end;
多维数组--单条记录
declare 
 type v_table is table of t_user%rowtype index by binary_integer;  
 my_table v_table;  
 begin 
       select * into my_table(9) from t_user where suser='admin';  
      --my_table(i) i可以为任意整数,但取值时必须保持以i一致;  
       dbms_output.put_line('--suser--'||my_table(9).suser||'--name--'||my_table(9).name);   
 end; 
 declare
 type v_table is table of t_user%rowtype index by binary_integer;
 my_table v_table;
 begin
       select * into my_table(9) from t_user where suser='admin';
       --my_table(i) i可以为任意整数,但取值时必须保持以i一致;
       dbms_output.put_line('--suser--'||my_table(9).suser||'--name--'||my_table(9).name);
 end;
--自定义数组
create or replace type varray_list as varray(30) of varchar2(50);  
 --使用自定义数组  
 create or replace procedure show_list(p_varlist in varray_list)  
 is 
 v_str varchar2(50);  
 begin 
       for i in 1..p_varlist.count   
       loop  
           v_str:=p_varlist(i);  
           dbms_output.put_line('v_str='||v_str);  
           dbms_output.put_line('p_varlist('||i||')='||p_varlist(i));  
       end loop;  
 end;
declare 
 my_var varray_list:=varray_list('g','m','d','龚','帅');  
 begin 
       show_list(my_var);   
 end;
实例
 代码如下 复制代码
--固定数组
declare
  type type_array is varray(10) of varchar2(20);
  var_array type_array:=type_array('ggs','jjh','wsb','csl','dd','bb');
begin
  for i in 1..var_array.count loop
      dbms_output.put_line(var_array(i));
  end loop;
end;
--可变数组
declare
  type type_array is table of varchar2(20) index by binary_integer;
  var_array type_array;
begin
  var_array(1):='aa';
  var_array(2):='bb';
for i in 1..var_array.count loop
     dbms_output.put_line( var_array(i));
  end loop;
end;
--可变数组取表
declare
begin
end;
create or replace procedure proc_stock(n number)
as    
       var_stock_code varchar2(10);
       var_stock_price number;
begin
       for i in 1..n loop
           var_stock_code:= lpad(str1 =>i ,len =>6 ,pad =>'0' ) ;
var_stock_price:=trunc(dbms_random.value*100)+1;
           --dbms_output.put_line(var_stock_code);
           --dbms_output.put_line(var_stock_price);
           insert into t_stock (stockcode,stockprice)
                  values(var_stock_code,var_stock_price);
           commit;      
       end loop;
end;
declare
begin
       proc_stock(1000000);
end;
--用游标访问 14.578秒 13.5 13.8
declare
       cursor cur is select * from t_stock;
       row_stock t_stock%rowtype;
begin
       open cur;
       loop
            fetch cur into row_stock;
            exit when cur%notfound;
            null;
       end loop;
       close cur;
end;
--用数组实现 4.813 1.953 2
declare
       type type_array is table of t_stock%rowtype index by binary_integer;
       var_array type_array;
begin
       select * bulk collect into var_array from t_stock;
       for i in 1..var_array.count loop         
           null;
       end loop;
end;
--访问自定义表
declare
       type type_record is record(
            username varchar2(20),
            sex varchar2(2)
       );
       type_record_user  type_record;
       type type_array is table of type_record_user%type index by binary_integer;
       var_array type_array;      
begin
       select username,sex bulk collect into var_array from tuser;
       for i in 1..var_array.count loop
           dbms_output.put_line(var_array(i).username);
           dbms_output.put_line(var_array(i).sex);
       end loop;
end;
关于oracle中的数组:记录同集合
集合可以有三种实现方式:
1 自定义一个type使用varray来得到一个数组但只能对基本类型定义如:
create type 类型名 as varray of varchar2(20);
1 自定义一个type使用varray来得到一个数组但只能对基本类型定义如:
create type 类型名 as varray(52) of varchar2(20);
不能使用如下:
create type 类型名 as varray(52) of 表名%rowtype;
注意:使用varray时一定要先指定数组大小
不然搞创建数组类型
2 内嵌表如:
 type 类型名 is table of 具体类型如:(表名%rowtype);
 内嵌表数组分二种:index_by表同嵌套表如上的就是嵌套表而index_by表只要在其尾回上 index by
binary_integer就可以了
例子:
 代码如下 复制代码
declare
cursor cur_test is select id,mc from test;
type t_test1 is table of varchar2(60) index by binary_integer;
type t_test2 is table of test%rowtype index by binary_integer;
var_test1 t_test1;
var_test2 t_test2;
var_new t_test2;
begin
select id,mc into var_test2(0) from test where id='111';
dbms_output.put_line('var_test2(0):'||var_test2(0).id||'---'||var_test2(0).mc);
select id,mc into var_test2(8) from test where id='333';
dbms_output.put_line('var_test2(8):'||var_test2(8).id||'---'||var_test2(8).mc);
var_new := var_test2;
dbms_output.put_line('===== copy var_test2 to var_new =====');
dbms_output.put_line('var_new(0):'||var_new(0).id||'---'||var_new(0).mc);
dbms_output.put_line('var_new(8):'||var_new(8).id||'---'||var_new(8).mc);
end;
===================================================================================
declare
type t_test1 is table of test.id%type;
type t_test2 is varray (10) of test.id%type;
var_test1 t_test1;
var_test2 t_test2;
begin
--var_test1(1) := ('test1.1'); --没有初始化不能赋值
var_test1 := t_test1('test1.1','test1.2','test1.3');
dbms_output.put_line('var_test1: '||var_test1(1)||','||var_test1(2)||','||var_test1(3));
var_test2 := t_test2('test2.1','test2.2','test2.3');
dbms_output.put_line('var_test2: '||var_test2(1)||','||var_test2(2)||','||var_test2(3));
var_test1(2) := 'test1.2_update';
dbms_output.put_line('==== 修改了var_test1(2) ====');
dbms_output.put_line('var_test1: '||var_test1(1)||','||var_test1(2)||','||var_test1(3));
dbms_output.put_line(var_test1.next(3));
dbms_output.put_line('var_test2元素个数: '||var_test2.limit());
end;
嵌套表的元素可以是集合,注意赋值的时候是varray_element.record_column := 的形式.
除了构造函数外,集合还有很多内建函数,按照面向对象编成的叫法称之为方法。
方法==========描述====================================================================使用限

count=========返回集合中元素的个数
delete========删除集合中所有元素
delete(x)=====删除元素下标为x的元素===================================================对
varray非法
delete(x,y)===删除元素下标从x到y的元素================================================对
varray非法
exist(x)======如果集合元素x已经初始化,则返回true, 否则返回false
extend========在集合末尾添加一个元素==================================================对
index_by非法
extend(x)=====在集合末尾添加x个元素===================================================对
index_by非法
extend(x,n)===在集合末尾添加元素n的x个副本============================================对
index_by非法
first=========返回集合中的第一个元素的下标号,对于varray集合始终返回1。
last==========返回集合中最后一个元素的下标号, 对于varray返回值始终等于count.
limit=========返回varry集合的最大的元素个数
===========================================index_by集合和嵌套表无用
next(x)=======返回在第x个元素之后及紧挨着它的元素的值,如果x是最后一个元素,返回null.
prior(x)======返回在第x个元素之前紧挨着它的元素的值,如果x是第一个元素,则返回null。
trim==========从集合末端开始删除一个元素==============================================对于
index_by不合法
trim(x)=======从集合末端开始删除x个元素===============================================对
index_by不合法
********************************************************************************************
记录可以定义为:
type 类型名 is recorder (具休类型)
也可用:变量名 表名%rowtype
例子:
隐式定义记录中,我们不用描述记录的每一个域,在声明记录变量时使用%rowtype命令定义与表,
视图,游标有相同结构的记录。
有一些pl/sql指令在使用隐式定义记录时没有使用%rowtype属性,比如游标for循环或中的:old
和:new记录
 代码如下 复制代码
declare
t_record1 test%rowtype;
cursor cur_test(v_id in varchar2) is
select id,mc from test
where id t_record2 cur_test%rowtype;
begin
for row_test in cur_test('333') loop
t_record1.id := row_test.id;
t_record1.mc := row_test.mc;
t_record2.id := row_test.id;
t_record2.mc := row_test.id;
dbms_output.put_line('t_record1:'||t_record1.id||'---'||t_record1.mc);
dbms_output.put_line('t_record2:'||t_record2.id||'---'||t_record2.mc);
dbms_output.put_line('row_test:'||row_test.id||'---'||row_test.mc);
dbms_output.put_line('================loop '||cur_test%rowcount||' times.');
end loop;
exception when others then
dbms_output.put_line(sqlcode||sqlerrm);
end;
======================================================================================
declare
type t_record is record
(
id test.id%type,
mc test.mc%type
);
var_record t_record;
counter number default 0;
begin
for row_test in (select id,mc from test) loop
counter := counter + 1;
var_record.id := row_test.id;
var_record.mc := row_test.mc;
dbms_output.put_line('var_record:'||var_record.id||'---'||var_record.mc);
dbms_output.put_line('row_test:'||row_test.id||'---'||row_test.mc);
dbms_output.put_line('================loop '||counter||' times.');
end loop;
exception when others then
dbms_output.put_line(sqlcode||sqlerrm);
end;
三、综合实例bulk collect的用法
 代码如下 复制代码
*/
set serverout on
declare
type t_record is record (
id number(18,0),
mc varchar2(50)
);
var_record t_record;
type t_test is table of t_record;
var_test t_test := t_test();
cursor cur_test is select id,mc from test;
begin
open cur_test;
fetch cur_test bulk collect into var_test;
for i in 1..var_test.count() loop
dbms_output.put_line(var_test(i).id||'---'||var_test(i).mc);
end loop;
end;
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product