Collections

WooBuntu·2021년 10월 4일
0

알고쓰자 PL/SQL

목록 보기
8/8

https://www.udemy.com/course/plsql-by-example/learn/lecture/3226722#overview

Collections

Associative Array

DECLARE
    TYPE customer_type IS TABLE OF VARCHAR2(100) INDEX BY BINARY_INTEGER;
    
    customer_table customer_type;
    
    v_idx NUMBER;
BEGIN
    customer_table(1):='MIKE';
    customer_table(2):='JEFF';
    customer_table(3):='JOHN';
    customer_table(6):='KING';
    
    customer_table.DELETE(3);
    
    v_idx:=customer_table.FIRST;
    
    WHILE v_idx IS NOT NULL LOOP
        DBMS_OUTPUT.PUT_LINE('Customer name: ' || customer_table(v_idx)); 
        v_idx:=customer_table.NEXT(v_idx);
        -- 1 => 2 => 6의 순서로 순회한다.
    END LOOP display_loop;
    
END;

NESTED TABLES

DECLARE
    TYPE customer_type IS TABLE OF VARCHAR2(100);
    
    customer_table customer_type:=customer_type();
    
    v_idx NUMBER;
BEGIN
    customer_table.EXTEND (4);
    -- 테이블을 사용하기 전에 반드시 EXTEND해야 한다.
    
    customer_table(1):='MIKE';
    customer_table(2):='JEFF';
    customer_table(3):='JOHN';
    -- customer_table(6):='KING';
    -- 에러 발생
    customer_table(4):='KING';
    
    customer_table.DELETE(3);
    
    v_idx:=customer_table.FIRST;
    
    WHILE v_idx IS NOT NULL LOOP
        DBMS_OUTPUT.PUT_LINE('Customer name' || customer_table(v_idx));
        v_idx:=customer_table.NEXT(v_idx);
        
   END LOOP display_loop;
END;

VARRAYS

DECLARE
    TYPE customer_type IS VARRAY(4) OF VARCHAR2(100);
    customer_table customer_type:=customer_type();
    v_idx NUMBER;
BEGIN
    customer_table.EXTEND(4);
    
    customer_table(1):='MIKE';
    customer_table(2):='JEFF';
    customer_table(3):='JHON';
    customer_table(4):='KING';
    
    -- varray에는 delete메서드가 존재하지 않는다.
    
    v_idx:=customer_table.FIRST;
    
    WHILE v_idx IS NOT NULL LOOP
        DBMS_OUTPUT.PUT_LINE('Customer name' || customer_table(v_idx));
        v_idx:=customer_table.NEXT(v_idx);
    END LOOP display_loop;
    
END;

Collection Methods

  • EXISTS(index)

  • COUNT

  • LIMIT

    VARRAY의 경우 최대 원소의 개수를 반환하고, nested table의 경우 null을 반환한다
    (왜?)

  • FIRST

  • LAST

  • PRIOR(index)

    NEXT반대

  • NEXT(index)

  • EXTEND

    collection에 null 원소 하나 추가

  • EXTEND(index)

    collection에 null 원소 index개 추가

  • EXTEND(n1, n2)

    n2를 n개 추가

  • TRIM

    마지막 원소 삭제

  • TRIM(n)

    뒤에서 n개의 원소 삭제

  • DELETE

    모든 원소 삭제

  • DELETE(index)

    index에 해당하는 원소를 삭제

  • DELETE(index1, index2)

    index1 ~ index2까지의 원소 삭제

DECLARE
    TYPE t_tab IS TABLE OF NUMBER;
    l_tab1 t_tab := t_tab(1,2,3,4,5,6);
    l_tab2 t_tab := t_tab(5,6,7,8,9,10);
BEGIN
    l_tab1 := l_tab1 MULTISET UNION DISTINCT l_tab2;
    -- MULTISET UNION (DISTINCT) : 합집합
    -- MULTISET EXCEPT : 차집합
    -- MULTISET INTERSECT : 교집합
    FOR i IN l_tab1.first .. l_tab1.last LOOP
        DBMS_OUTPUT.PUT_LINE(l_tab1(i));
    END LOOP;
END;

0개의 댓글