ABOUT ME

Today
Yesterday
Total
  • oracle PL/SQL_tableType 변수
    DataBase/oracle 2020. 3. 23. 01:30

     

    Table Type 변수 (=컬렉션)

    컬렉션 : 일반 프로그래밍 언어에서 사용하는 배열타입을 PL/SQL에서는 컬렉션이라고한다!

       1. 종류

          (1) 연관배열 ( associative array / index-by table ) : key 와 value 를 같이 갖고있는 것 

              1) key의 데이터 유형 :

                   -->숫자 : binary_integer, pls_integer ( 두 데이터타입은 number보다 작은 저장영역이 필요

                               산술 연산의 경우 number보다 빠르다

                   --> 문자 : varchar2

              2) value의 데이터 유형 : 일반 데이터 타입, 레코드(=여러개의 값을 가질 수 있다)          

          (2) varray (=variable array) : 고정길이를 가진 배열, 일반 프로그래밍에서 사용하는 배열과 같다,

                                               순서O, 0부터 시작

          (3) 중첩테이블 (=nested table) : 배열의 크기를 명시하지않고 동적으로 배열의 크기가 결정됨, 순서X, 

       

       2. Table Type 선언형식

          1. 정의

              Type 타입명 IS TABLE OF

              ex) employees.first_name%type

              INDEX BY BINARY_INTEGER;

          2. 선언 (= 메모리화)

              별자 타입명;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    declare
        tname varchar2(20);
        
        --정의
        Type t_emp_name is table of
        employees.last_name%type
        index by binary_integer;  --배열형식이므로 index가 필요한데 binary_integer타입의 index이다
        
        --선언
        v_name t_emp_name; -- v_name 은 테이블타입의 변수
     
    begin
        
        select last_name into tname
        from employees 
        where employee_id = 100;
        
        v_name(0) := tname;    
        
        dbms_output.put_line(v_name(0));
     
    end;
     
    /
     
     
    http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

     

    반복문을 이용하여 배열 출력

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
     
    declare
        
        type tbl_type is table of
        employees.last_name%type
        index by binary_integer;
        
        vtbl_type tbl_type;
        
        --binary_integer 값을 0으로 초기화
        --i 라는 변수의 값을 초기화
        i binary_integer := 0;
        
    begin
     
        for emp_name in (select last_name from employees) loop
        i:= i+1;
        vtbl_type(i) := emp_name.last_name; 
        end loop;
        for j in 1..i loop -- 변수 j를 1부터 i까지 돌리겠다
            dbms_output.put_line(vtbl_type(j));
        end loop;
     
    end;
     
    /
     
     
     
    http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

     

    -- 바인트 변수 (비 PL/SQL 변수)
    -- 호스트 환경에서 생성되어 데이터를 저장하기때문에 호스트변수라고 말한다 <- 변수를 선언하지않고 바로사용가능
    -- 키워드 VARIABLE을 이용하며, SQL문이나 PL/SLQ블록에서도 사용가능
    -- PL/SQL 블록이 실행된 후에도 엑세스가 가능하다
    -- print 명령을 이용하여 출력이 가능하다
    -- :을 붙여 이용한다

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    set autoprint on-- pl블록 실행후 자동으로 print 실행
     
    begin
        select (salary*12+nvl(commission_pct*salary,0)) into :vsal 
        from employees
        where employee_id = 100;
        
    end;
    /
     
    print vsal; --vsal은 출력되는데 값은 출력 안된다...뭐지
     
     
     
    http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

    'DataBase > oracle' 카테고리의 다른 글

    댓글

Designed by Tistory.