2018. 3. 7. 14:09
반응형

cursor가 반환되는 procedure를 실행하는 방법


1. 정석 (cursor를 record에 담아서 loop 처리)

SET SERVEROUTPUT ON; -- dbms output 결과 출력

DECLARE

  t_cursor 실행프로시져.t_cursor; -- 실행프로시져에서 이미 cursor type을 지정해준 경우 가져다 쓴다. 지정 안되어 있으면 cursor type을 새로 지정해야됨.

  -- type t_cursor_type is ref cursor;

  -- t_cursor t_cursor_type;

  input1 number;

  input2 varchar2(2000);

type res_rec_type

is

  record

  (

    field1  number,

    field2  varchar2(200)

);

  res_rec res_rec_type;

  -- 커서를 저장할 record는 테이블 구조와 동일하면 %ROWTYPE도 사용 가능함.

  -- res_rec 테이블명%ROWTYPE;

  

  -- 2번째 테스트용

  field1  number;

  field2  varchar2(200);

  -- 변수 타입에 대해서 테이블 필드의 타입으로 사용 가능 -> %TYPE

  -- field1  테이블명.필드명%type;

BEGIN

  실행프로시져('1', '1', t_cursor, input1, input2) ;

  dbms_output.put_line('res : ' || input1 || ':' ||input2);

  

  -- record를 이용해도 되고 각 필드를 넣어 줘도 값이 할당 된다.

  -- 1번째 테스트

  LOOP

    FETCH t_cursor INTO res_rec;

    EXIT

  WHEN t_cursor%notfound;

    dbms_output.put_line(res_rec.field1 || ',' || res_rec.field2);

  END LOOP;

  

  -- 2번째 테스트

  LOOP

    FETCH t_cursor INTO field1, field2;

    EXIT

  WHEN t_cursor%notfound;

    dbms_output.put_line(field1 || ',' || field2);

  END LOOP;

END;


http://deviant86.tistory.com/466 이곳에 상세 설명이 잘 되어 있는것 같다.



2. 간단하게 결과값을 보여주고 cursor값을 print함.

set serveroutput on 

var t_cursor refcursor;

declare

  input1    number;

  input2    varchar2(2000);

begin

  실행프로시져('1', '1', :t_cursor, input1, input2) ;

  dbms_output.put_line(input1);

end;

/

print t_cursor;


3. execute후 print

var t_cursor refcursor;

var input1 number;

var input2 varchar2(2000);

exec 실행프로시져('1', '1', :t_cursor, :input1, :input2) ;

print t_cursor;

print input1;

print input2;


4. 선언없이 print 처리

exec 실행프로시져('1', '1', :t_cursor, :input1, :input2) ;

print t_cursor;

print input1;

print input2;


loop를 돌려서 상세 디버깅이 필요한 경우 1번을 사용하고

간단하게 결과값만 확인하기 위해선 4번을 사용하면 될 것 같다.


반응형
Posted by seongsland