TIL 23.09.12

ν™©μ€ν•˜Β·2023λ…„ 9μ›” 12일
0

TIL

λͺ©λ‘ 보기
79/146

πŸ“ŒToday I Learned

였라클 μˆ˜μ—…

ex_pl/sql

ν•¨μˆ˜ return

  1. 단일값 O
  2. 닀쀑값 X > cursor

ν”„λ‘œμ‹œμ € out parameter

  1. 단일값(단일 λ ˆμ½”λ“œ)
    a. number
    b. varchar2
    c. date
  2. 닀쀑값(닀쀑 λ ˆμ½”λ“œ)
    a. cursor
create or replace procedure procBuseo (
    pbuseo in varchar2,
    pcursor out sys_refcursor --μ»€μ„œμ˜ μžλ£Œν˜• 
)
is
    -- cursor vcursor is select..   > λ‚΄λΆ€ μ†ŒλΉ„ μ‹œ μ΄λ ‡κ²Œ μ‚¬μš©
begin
    open pcursor
    for 
    select * from tblInsa where buseo = pbuseo;
end procBuseo;


declare
    vcursor sys_refcursor; -- μ»€μ„œ μ°Έμ‘°λ³€μˆ˜
    vrow tblInsa%rowtype;
begin
    procBuseo('μ˜μ—…λΆ€', vcursor);
    loop
        fetch vcursor into vrow;
        exit when vcursor%notfound;
        
        -- 업무
        dbms_output.put_line(vrow.name);
    end loop;
end;

ν”„λ‘œμ‹œμ € 총 정리 > CRUD

-- 1. μΆ”κ°€ μž‘μ—…(C)
create or replace procedure μΆ”κ°€μž‘μ—… (
    μΆ”κ°€ν•  데이터 -> in λ§€κ°œλ³€μˆ˜,
    μΆ”κ°€ν•  데이터 -> in λ§€κ°œλ³€μˆ˜,
    μΆ”κ°€ν•  데이터 -> in λ§€κ°œλ³€μˆ˜, -- μ›ν•˜λŠ” 만큼
    성곡 유무 λ°˜ν™˜-> out λ§€κ°œλ³€μˆ˜ -- ν”Όλ“œλ°±(1, 0)
)
is
    λ‚΄λΆ€ λ³€μˆ˜ μ„ μ–Έ
begin
    μž‘μ—…(insert + (select, update, delete))
exception
    when others then
        μ˜ˆμ™Έμ²˜λ¦¬
end μΆ”κ°€μž‘μ—…;


-- ν•  일 μΆ”κ°€ν•˜κΈ°(C)
create or replace procedure procAddTodo(
    ptitle varchar2,
    presult out number -- 1 or 0
)
is
begin
    insert into tblTodo (seq, title, adddate, completedate) values (seqTodo.nextVal, ptitle, sysdate, null);
    
    presult := 1; -- 성곡
exception
    when others then
        presult := 0;
end procAddTodo;


select * from tbltodo; -- 24

create sequence seqTodo start with 25;

declare
    vresult number;
begin
    procAddTodo('μƒˆλ‘œμš΄ ν•  μΌμž…λ‹ˆλ‹€.', vresult);
    dbms_output.put_line(vresult);
end;


-- 2. μˆ˜μ • μž‘μ—…(U)
create or replace procedure μˆ˜μ •μž‘μ—…(
    μˆ˜μ •ν•  데이터 -> in λ§€κ°œλ³€μˆ˜,
    μˆ˜μ •ν•  데이터 -> in λ§€κ°œλ³€μˆ˜,
    μˆ˜μ •ν•  데이터 -> in λ§€κ°œλ³€μˆ˜, -- μ›ν•˜λŠ” 개수
    μ‹λ³„μž       -> in λ§€κ°œλ³€μˆ˜,  -- whereμ ˆμ— μ‚¬μš©ν•  PK or 데이터 (쑰건)
    성곡 유무 λ°˜ν™˜-> out λ§€κ°œλ³€μˆ˜ -- ν”Όλ“œλ°±(1, 0)
)
is
    λ‚΄λΆ€ λ³€μˆ˜ μ„ μ–Έ
begin
    μž‘μ—…(update + (insert, update, delete, select...))
exception
    when others then
        μ˜ˆμ™Έμ²˜λ¦¬
end;


-- ν•  일 μˆ˜μ •ν•˜κΈ°(U) > completedate > μ±„μš°κΈ° > ν•  일 μ™„λ£Œ μ²˜λ¦¬ν•˜κΈ°
create or replace procedure procCompleteTodo(
    -- pcompletedate date > μˆ˜μ •ν•  λ‚ μ§œ > μ§€κΈˆ > sysdate 처리
    pseq in number, -- μˆ˜μ •ν•  ν•  일 번호
    presult out number
)
is
begin
    update tblTodo set 
        completedate = sysdate 
            where seq = pseq;
            
    presult := 1;
exception
    when others then
        presult := 0;
end procCompleteTodo;


declare
    vresult number;
begin
    procCompleteTodo(25, vresult);
    dbms_output.put_line(vresult);
end;

select * from tbltodo;


-- 3. μ‚­μ œ μž‘μ—…(D)
create or replace procedure μ‚­μ œμž‘μ—… (
    μ‹λ³„μž        -> in λ§€κ°œλ³€μˆ˜,
    성곡 유무 λ°˜ν™˜ -> out λ§€κ°œλ³€μˆ˜
)
is
    λ‚΄λΆ€ λ³€μˆ˜ μ„ μ–Έ
begin
    μž‘μ—…(delete + (insert, update, delete, select))
exception
    when others then
        μ˜ˆμ™Έμ²˜λ¦¬
end;


create or replace procedure procDeleteTodo(
    pseq in number,
    presult out number
)
is
begin
    delete from tblTodo where seq = pseq;
    presult := 1;
exception
    when others then
        presult := 0;
end procDeleteTodo;


declare
    vresult number;
begin
    procDeleteTodo(25, vresult);
    dbms_output.put_line(vresult);
end;

select * from tblTodo;



-- 4. 읽기 μž‘μ—…(R)
-- : 쑰건 유/무
-- : λ°˜ν™˜ 단일행/닀쀑행, λ‹¨μΌμ»¬λŸΌ/λ‹€μ€‘μ»¬λŸΌ

create or replacr procedure μ½κΈ°μž‘μ—…(
    쑰건 데이터 -> int λ§€κ°œλ³€μˆ˜,
    단일 λ°˜ν™˜κ°’ -> out λ§€κ°œλ³€μˆ˜,
    닀쀑 λ°˜ν™˜κ°’ -> out λ§€κ°œλ³€μˆ˜(μ»€μ„œ)
)
is
    λ‚΄λΆ€ λ³€μˆ˜ μ„ μ–Έ
begin
    μž‘μ—…(select + (insert, update, delete, select))
exception
    when others then
        μ˜ˆμ™Έμ²˜λ¦¬
end;


-- ν•œ 일 λͺ‡ 개? μ•ˆ ν•œ 일 λͺ‡ 개? 총 λͺ‡ 개?
create or replace procedure procCountTodo(
    pcount1 out number, -- ν•œ 일
    pcount2 out number, -- μ•ˆ ν•œ 일
    pcount3 out number  -- λͺ¨λ“  일
)
is
begin
    select count(*) into pcount1 from tblTodo where completedate is not null;
    select count(*) into pcount2 from tblTodo where completedate is null;
    select count(*) into pcount3 from tblTodo;
exception
    when others then
        dbms_output.put_line('μ˜ˆμ™Έ 처리'); -- μ‹€μ œλŠ” 둜그 남김
end procCountTodo;


declare
    vcount1 number;
    vcount2 number;
    vcount3 number;
begin
    procCountTodo(vcount1, vcount2, vcount3);
    dbms_output.put_line(vcount1);
    dbms_output.put_line(vcount2);
    dbms_output.put_line(vcount3);
end;


create or replace procedure procCountTodo(
    psel in number,     -- 선택(1(ν•œ 일), 2(μ•ˆ ν•œ 일), 3(λͺ¨λ“  일))
    pcount out number
)
is
begin
    if psel = 1 then
        select count(*) into pcount from tblTodo where completedate is not null;
    elsif psel = 2 then
        select count(*) into pcount from tblTodo where completedate is null;
    elsif psel = 3 then
        select count(*) into pcount from tblTodo;
    end if;
exception
    when others then
        dbms_output.put_line('μ˜ˆμ™Έ 처리'); -- μ‹€μ œλŠ” 둜그 남김
end procCountTodo;

declare
    vcount number;
begin
    procCountTodo(3, vcount);
    dbms_output.put_line(vcount);
end;


-- 번호 > ν•  일 1개 λ°˜ν™˜
create or replace procedure procGetTodo(
    pseq in number,
    prow out tblTodo%rowtype
)
is
begin
    select * into prow from tblTodo where seq = pseq;
exception
    when others then
        dbms_output.put_line('μ˜ˆμ™Έ');
end procGetTodo;


declare
    vrow tblTodo%rowtype;
begin
    procGetTodo(1, vrow);
    dbms_output.put_line(vrow.title);
end;

select * from tblTodo;



-- 닀쀑 λ ˆμ½”λ“œ λ°˜ν™˜
-- 1. ν•œ 일 λͺ©λ‘ λ°˜ν™˜
-- 2. μ•ˆ ν•œ 일 λͺ©λ‘ λ°˜ν™˜
-- 3. λͺ¨λ“  일 λͺ©λ‘ λ°˜ν™˜

create or replace procedure procListTodo(
    psel in number,
    pcursor out sys_refcursor
)
is 
begin
    if psel = 1 then
        open pcursor
        for
        select * from tblTodo where completedate is not null;
    elsif psel = 2 then
        open pcursor
        for
        select * from tblTodo where completedate is null;
    elsif psel = 3 then
        open pcursor
        for
        select * from tblTodo;
    end if;
exception
    when others then
        dbms_output.put_line('μ˜ˆμ™Έ');
end procListTodo;

declare
    vcursor sys_refcursor;
    vrow tblTodo%rowtype;
begin
    procListTodo(3, vcursor);
    loop
        fetch vcursor into vrow;
        exit when vcursor%notfound;
        
        dbms_output.put_line(vrow.title || ', ' || vrow.completedate);
        
    end loop;
end;

DB ν”„λ‘œμ νŠΈ

λ°μ΄ν„°λ² μ΄μŠ€μ— λ“€μ–΄κ°ˆ 더미데이터λ₯Ό λ§Œλ“€μ—ˆλ‹€. 그리고 ν…Œμ΄λΈ” μ •μ˜μ„œ(DDL)도 ν•¨κ»˜ μž‘μ„±ν•˜μ˜€λ‹€.

profile
μ°¨κ·Όμ°¨κ·Ό ν•˜λ‚˜μ”©

0개의 λŒ“κΈ€