- 1νμ© μ½λ μμ±μ©
- λ°μ΄ν°λ² μ΄μ€ κ°μ²΄
- μ μ₯μ©
- μ¬νΈμΆ
μλ£ν + λ³μ
PL/SQL μλ£ν
λ³μ μ μΈνκΈ°
PL/SQL μ°μ°μ
λμ μ°μ°μ
λ³μ > μ΄λ€ μ©λλ‘ μ¬μ©?
- select κ²°κ³Όλ₯Ό λ΄λ μ©λ(맀μ°μ€μ)
- select into μ (PL/SQL)
PL/SQL νλ‘μμ μμμλ μμν SELECTλ¬Έμ μ¬ μ μλ€.(μ λ)
PL/SQL νλ‘μμ μμμλ SELECT INTOλ¬Έλ§ μ¬μ©νλ€.
intoλ νλ²λ§.
into μ¬μ© μ
1. 컬λΌμ κ°μμ λ³μμ κ°μ μΌμΉ
2. 컬λΌμ μμμ λ³μμ μμ μΌμΉ
3. 컬λΌκ³Ό λ³μμ μλ£ν μΌμΉ
%type
%rowtype
μ μ΄λ¬Έ
1. 쑰건문
2. λ°λ³΅λ¬Έ
3. λΆκΈ°λ¬Έ
case문
λ°λ³΅λ¬Έ
declare
vnum number := 1;
begin
loop
dbms_output.put_line(vnum);
vnum := vnum + 1;
exit when vnum > 10; -- μ‘°κ±΄λΆ break
end loop;
end;
begin
for dan in 2..9 loop
for num in 1..9 loop
insert into tblgugudan (dan, num, result) values (dan, num, dan * num);
end loop;
end loop;
end;
declare
vnum number := 1;
begin
while vnum <= 10 loop
dbms_output.put_line(vnum);
vnum := vnum + 1;
end loop;
end;
select > κ²°κ³Όμ > PL/SQL λ³μ λμ
declare
λ³μ μ μΈ;
컀μ μ μΈ;
begin
컀μ μ΄κΈ°;
loop
λ°μ΄ν° μ κ·Ό(루ν 1νμ > λ μ½λ 1κ°) <- 컀μ μ¬μ©
end loop;
컀μ λ«κΈ°;
end;
--------------------------------------------------------
-- κ± μΈμ.
declare
cursor vcursor
is
select * from tblInsa;
vrow tblInsa%rowtype;
begin
open vcursor;
loop
fetch into;
exit when;
end loop;
close vcursor;
end;
-- μ μ
declare
cursor vcursor
is
select * from tblInsa;
vrow tblInsa%rowtype;
begin
open vcursor;
loop
fetch vcursor into vrow;
exit when vcursor%notfound;
dbms_output.put_line(vrow.name);
end loop;
close vcursor;
end;
-- κ°κ²°
declare
cursor vcursor
is
select * from tblInsa;
vrow tblInsa%rowtype;
begin
for vrow in vcursor loop -- loop + fetch into + vrow + exit when (for 루νλ³μ in μ§ν© loop)
dbms_output.put_line(vrow.name);
end loop;
end;
μμΈμ²λ¦¬
: μ€νλΆ(begin-end)μμ λ°μνλ μμΈλ₯Ό μ²λ¦¬νλ λΈλ > exception λΈλ
-- μμΈ λ°μ > DB μ μ₯
create table tblLog(
seq number primary key, -- PK
code varchar2(7) not null check (code in ('A001', 'B001', 'B002', 'C001')), --μλ¬ μν μ½λ
message varchar2(1000) not null, -- μλ¬ λ©μμ§
regdate date default sysdate not null -- μλ¬ λ°μ μκ°
);
create sequence seqLog;
select * from tblLog;
declare
vcnt number;
vname tblInsa.name%type;
begin
select count(*) into vcnt from tblCountry where name = 'νκ΅'; -- ORA-01476: divisor is equal to zero
dbms_output.put_line(100 / vcnt);
select name into vname from tblInsa where num = 1000; -- ORA-01403: no data found
dbms_output.put_line(vname);
exception
when ZERO_DIVIDE then
dbms_output.put_line('0μΌλ‘ λλκΈ°');
insert into tblLog values (seqLog.nextVal, 'B001', 'κ°μ Έμ¨ λ μ½λκ° μμ΅λλ€.', default);
when NO_DATA_FOUND then
dbms_output.put_line('λ°μ΄ν° μμ');
insert into tblLog values (seqLog.nextVal, 'A001', 'μ§μμ΄ μ‘΄μ¬νμ§ μμ΅λλ€.', default);
when others then
dbms_output.put_line('λλ¨Έμ§ μμΈ ');
insert into tblLog values (seqLog.nextVal, 'C001', 'κΈ°ν μμΈκ° λ°μνμ΅λλ€.', default);
end;