ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • oracle PL/SQL_ 커서(cursor)
    DataBase/oracle 2020. 3. 29. 15:24

     

    커서 (cursor) : 쿼리문에의해 반환되는 결과값들을 저장하는 메모리 공간

    패치 (Fatch)  : 커서에서 원하는 결과값을 추출하는 것 

     

    1. 커서의 종류

    1) 명시적(Explicit) 커서 : 사용자가 선언하여 생성 후 사용하는 sql커서. 

                                    주로 여러개의 행을 처리하고자 할 때 사용.

     

    2) 묵시적(implicit) 커서 : 오라클에서 자동으로 선언해주는 sql커서.

                                     사용자는 생성 유무를 알 수 없다.

     

    2. 커서의 속성

    1) %Found  : 할당할 레코드가 있는경우 true값 반환

    2) %isOpen : 커서가 오픈상태일 경우 true값 반환

    3) %NotFound : 할당할 레코드가 없는 경우 true값 반환

    4) %RowCount : 카운터역할을 한다. 오픈됐을경우 0, 패치 발 생 때마다 1씩 증가

     

    3. 커서의 처리 단계 (명시적 커서) 

    1) 명시적 커서 선언            -- Cursor 커서이름

    2) 명시적 커서 오픈            -- Open 커서이름

    3) 커서에서 데이터추출       -- Fetch 커서이름

    4) 커서 종료                      -- Close 커서이름

     

    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
    30
    31
    32
    declare
        emp_id number;
        emp_name varchar2(20);
        emp_salary number(20);
        
        -- 명시적 커서 선언
        Cursor cu1 is 
        select employee_id, last_name, salary
        from employees
        where department_id = 50;
        
    begin
        -- 명시적 커서 오픈
        open cu1;
        dbms_output.put_line ('사번   이름  급여');
        
        loop
        -- 커서에서 데이터 추출
        fetch cu1 into emp_id, emp_name, emp_salary;
        
        -- 조건 ( notFound : 변수에 할당 할 값이 없을 때 loop를 빠져나가겠다)
        exit when cu1%notFound; 
       dbms_output.put_line (emp_id ||' '||emp_name || ' ' || emp_salary );
     
        
        end loop;
        
        -- 커서 닫기
        close cu1;
        
    end;
    /
    http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

     

    -- 커서와 반복문 (for문)

        사용형식

        For 레코드 네임 IN 커서이름 LOOP

    ** 명시적 커서의 OPEN, FETCH가 자동으로 수행

           실행문장들

        END LOOP ; 

    ** 루프문을 빠져나갈 떄 자동으로 커서가 종료됨

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    declare
        Cursor emp_cur is
        select employee_id, last_name
        from employees;
    begin
        for emp_rec in emp_cur loop
           dbms_output.put_line (emp_rec.employee_id || ' ' || emp_rec.last_name);
        end loop;
    end;
    /
    http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

     

    --rowType변수 사용하기

    ** 레코드 이름을 정의하지않고 rowtype변수로 사용할 수 있다

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
     
    declare
        emp employees%rowtype;
        
        cursor cur1 is
        select employee_id, last_name, salary
        from employees;
        
    begin
        for emp in cur1 loop
        exit when cur1%notfound;
        dbms_output.put_line (emp.employee_id || ' ' || emp.last_name || ' '|| emp.salary );
     
        end loop;
    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
    declare
    begin
        for emp in (select employee_id, last_name, salary from employees)
        loop
        dbms_output.put_line (emp.employee_id || ' ' || emp.last_name || ' '|| emp.salary );
        end loop;
     
    end;
    /
    http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

     

    4. 묵시적 커서의 사용 예

    선언을 하지않기때문에 속성 사용시 sql%notfound, sql%found, sql%rowcount, sql%isOpen형식으로 사용

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
     
    declare
     cnt1 number;
     cnt2 number;
     
    begin
        select count(*into cnt1
        from employees
        where department_id = 20;
        
        cnt2 := sql%rowcount;
        dbms_output.put_line ('cnt1의 값 : '||cnt1 );
        dbms_output.put_line ('cnt2의 값 : '||cnt2 );
     
    end;    
    /
    http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

    댓글

Designed by Tistory.