zoukankan      html  css  js  c++  java
  • pl/sqlcollection

    There are three kinds of collection for pl/sql:

    nested tables - covered in part 10 of this Oracle PLSQL tutorial

    2 varrays - covered in this tutorial

    3 PL/SQL associative arrays - covered in part 12 of this PL/SQL tutorial .

     1 http://www.smart-soft.co.uk/Oracle/oracle-plsql-tutorial-part-12.htm

       DECLARE

      CURSOR name_cur IS
      
    SELECT last_name
      
    FROM student
      
    WHERE rownum <= 10;
      
      
    --the nested table is initialized at the time of declaration
      --Notice that this declaration is very similar to the declaration of an associative array, except that
      --it has no INDEX BY BINARY_INTEGER clause.
      TYPE last_name_type IS TABLE OF student.last_name%TYPE;
      last_name_tab last_name_type :
    = last_name_type();
      
      v_counter 
    INTEGER := 0;
    BEGIN
      
    FOR name_rec IN name_cur LOOP
        v_counter :
    = v_counter + 1;
        
        
    --In the cursor loop is a statement with one of the collection methods,
        --EXTEND. This method allows you to increase the size of the collection.
        last_name_tab.EXTEND;
        
        last_name_tab(v_counter) :
    = name_rec.last_name;
        DBMS_OUTPUT.PUT_LINE (
    'last_name('||v_counter||'): '||
        last_name_tab(v_counter));
      
    END LOOP;
    END;

    http://www.smart-soft.co.uk/Oracle/oracle-plsql-tutorial-part-11.htm

    A good example of the use of these would be for storing telephone numbers or addresses for say employees - each person is unlikely to have more than half a dozen telephone numbers or addresses. We can create a table for storing these as follows:-

     TYPE phone_no_tab_typ IS VARRAY(6) OF VARCHAR2(20;

    phone_nos phone_no_tab_typ;

     TYPE varray_type IS VARRAY(10OF NUMBER;

    varray varray_type := varray_type(123456);

     another example as follows: 

    DECLARE

      CURSOR name_cur IS
      
    SELECT last_name
      
    FROM student
      
    WHERE rownum <= 10;
      
      TYPE last_name_type 
    IS VARRAY(10OF student.last_name%TYPE;
      last_name_varray last_name_type :
    = last_name_type();
      
      v_counter 
    INTEGER := 0;
    BEGIN
      
    FOR name_rec IN name_cur LOOP
        v_counter :
    = v_counter + 1;
        last_name_varray.EXTEND;
        last_name_varray(v_counter) :
    = name_rec.last_name;
        DBMS_OUTPUT.PUT_LINE (
    'last_name('||v_counter||'): '||
        last_name_varray(v_counter));
      
    END LOOP;
    END;

    http://www.smart-soft.co.uk/Oracle/oracle-plsql-tutorial-part-10.htm

       DECLARE

      CURSOR name_cur IS
      
    SELECT last_name
      
    FROM student
      
    WHERE rownum <= 10;
      
      TYPE last_name_type 
    IS TABLE OF student.last_name%TYPE
      
    INDEX BY BINARY_INTEGER;
      last_name_tab last_name_type;
      
      v_counter 
    INTEGER := 0;
    BEGIN
      
    FOR name_rec IN name_cur LOOP
        v_counter :
    = v_counter + 1;
        last_name_tab(v_counter) :
    = name_rec.last_name;
        DBMS_OUTPUT.PUT_LINE (
    'last_name('||v_counter||
        
    '): '||last_name_tab(v_counter));
      
    END LOOP;
      
      DBMS_OUTPUT.PUT_LINE (
    'last_name(11): '||
      last_name_tab(
    11));
    END;

     Please pay attention to the index-by table of associative arrays:

    set serveroutput on;
    DECLARE
         type num_tab 
    is table of varchar2(1000
         
    index by binary_integer;
         
         v_example_tab num_tab;
         v_num 
    number:=13;
    BEGIN
         
        v_example_Tab(
    -10):='abbb';
        dbms_output.put_line(v_example_Tab(
    -10));
    END;
  • 相关阅读:
    Eclipse Alt + / 无提示
    洛谷 P1101 单词方阵
    力扣题解 7th 整数反转
    力扣题解 344th 反转字符串
    力扣题解 48th 旋转图像
    力扣题解 36th 有效的数独
    力扣题解 1th 两数之和
    力扣题解 283th 移动零
    力扣题解 66th 加一
    力扣题解 350th 两个数组的交集 II
  • 原文地址:https://www.cnblogs.com/kelin1314/p/1921533.html
Copyright © 2011-2022 走看看