Jam's story

[Oracle] days19 - 패키지 +파라미터 이용한 커서 본문

Oracle

[Oracle] days19 - 패키지 +파라미터 이용한 커서

애플쩀 2022. 4. 28. 10:08
패키지
  1. 관계되는 타입, 프로그램 객체, 서브 프로그램 (PROCEDURE, FUNCTION) 을 [논리적으로 묶어] 놓은 것
  2. 패키지는 specification과 body 부분으로 되어 있다.
  3. specification 부분은 type, constant, variable, exception, cursor, subprogram이 선언된다.
    body 부분은 cursor, subprogram 따위가 존재한다
  4. '패키지_이름.프로시저_이름' 형식의 참조를 이용

 

파라미터를 이용한 커서  (에러  )

create or replace procedure up_seldeptEmp
(
    pdeptno dept.deptno%type
    
)
is
    vename emp.ename%type;
    vsal emp.sal%type;
--명시적커서 - open loop~fetch close 
    cursor cemplist(cdeptno dept.deptno%type) is ( select ename, sal from emp where deptno=cdeptno; --파라미터로 들어오는 부서번호 ) 
begin
--처음 pdeptno가 들어오고, 그것을 cemplist에 전달하니 
--cursor에서 cdpetno로 설정되고, 그것이  is 뒷부분 cdeptno로 바꿔야함 
    open cemplist(pdeptno);
    loop 
    fetch cemplist into vename, vsal;
    exit when cemplist%notfound;
    dbms_output.put_line(vename || ',' || vsal);
    end loop;
    close cemplist;
end;

 

sys_refcursor

커서를 담을 수 있는 자료형 

 

create or replace procedure up_selinsa
(
pcursor sys_refcursor 
)
is
vname insa.name%type;
vcity insa.city%type;
vbasicpay insa.basicpay%type;
begin
    loop
    fetch  pcursor into vname,vcity, vbasicpay;
    exit when pcursor%notfound;
    dbms_output.put_line(vname || ',' || vcity || ',' || vbasicpay);
    end loop;
--exception 
end;

 

 

테스트 

create or replace procedure up_test_selinsa
    
is
    vcursor sys_refcursor;
begin
    open vcursor for select name, city, basicpay from insa;

    up_selinsa(vcursor);
    
    close vcursor; 
    

end;

 

 

 

출력용으로도 선언할 수 있다. 

exec up_test_selinsa;


create or replace procedure up_selinsa 
(
--출력용 매개변수 
    pcursor out sys_refcursor 
)
is
begin 
    open pcursor for select name, city, basicpay from insa; 
end; 

--jdbc=java+orale 에서 사용
Comments