zoukankan      html  css  js  c++  java
  • Oracle数组一般可以分为固定数组和可变数组

    可变数组 一维数组

    Sql代码
    1. declare 
    2. type v_table istableof varchar2(30) indexby binary_integer;  
    3. --类型可以是前面的类型定义,index by binary_integer子句代表以符号整数为索引,  
    4. --这样访问表类型变量中的数据方法就是“表变量名(索引符号整数)”。  
    5. my_table v_table;  
    6. begin 
    7.       for i in 1..20  
    8.       loop  
    9.           my_table(i):=i;  
    10.           dbms_output.put_line(my_table(i));  
    11.       end loop;  
    12. 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;
    

    多维数组--多条记录

    Sql代码
    1. declare 
    2. type v_table istableof t_user%rowtype indexby binary_integer;  
    3. my_table v_table;  
    4. begin 
    5.       select * bulk collect into my_table from t_user;  
    6.       for i in 1..my_table.count/10 --my_table.count/10取到的值为四舍五入值  
    7.       loop  
    8.           dbms_output.put_line('suser--'||my_table(i).suser);  
    9.           dbms_output.put_line('name---'||my_table(i).name);  
    10.           dbms_output.put_line('sex----'||my_table(i).sex);  
    11.       end loop;  
    12. 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;
    

    多维数组--单条记录

    Sql代码
    1. declare 
    2. type v_table istableof t_user%rowtype indexby binary_integer;  
    3. my_table v_table;  
    4. begin 
    5.       select * into my_table(9) from t_user where suser='admin';  
    6.       --my_table(i) i可以为任意整数,但取值时必须保持以i一致;  
    7.       dbms_output.put_line('--suser--'||my_table(9).suser||'--name--'||my_table(9).name);   
    8. 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;
    

    自定义数组

    Sql代码
    1. createorreplace type varray_list as varray(30) of varchar2(50);  
    2. --使用自定义数组  
    3. createorreplaceprocedure show_list(p_varlist in varray_list)  
    4. is 
    5. v_str varchar2(50);  
    6. begin 
    7.       for i in 1..p_varlist.count   
    8.       loop  
    9.           v_str:=p_varlist(i);  
    10.           dbms_output.put_line('v_str='||v_str);  
    11.           dbms_output.put_line('p_varlist('||i||')='||p_varlist(i));  
    12.       end loop;  
    13. end;  
    14.  
    15. declare 
    16. my_var varray_list:=varray_list('g','m','d','龚','帅');  
    17. begin 
    18.       show_list(my_var);   
    19. end
    待人以诚,做事用心,对事不对人.
  • 相关阅读:
    常用模块
    递归函数
    内置函数与匿名函数
    Mac控制台相关操作
    Maven相关知识记录
    @Import底层实现原理
    spring循环依赖
    springcloud注册中心对比
    分布式事务
    Drools使用注意事项
  • 原文地址:https://www.cnblogs.com/jiangguang/p/2761125.html
Copyright © 2011-2022 走看看