νλ‘μμ out parameter
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;
-- 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;
λ°μ΄ν°λ² μ΄μ€μ λ€μ΄κ° λλ―Έλ°μ΄ν°λ₯Ό λ§λ€μλ€. κ·Έλ¦¬κ³ ν μ΄λΈ μ μμ(DDL)λ ν¨κ» μμ±νμλ€.