์ƒˆ์†Œ์‹

IT & Security/DB

[Oracle] ํ”„๋กœ์‹œ์ € | ์‚ฌ์šฉ์ž ์ •์˜ํ•จ์ˆ˜ | ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ

  • -
๋ฐ˜์‘ํ˜•

*๋ฌด๊ฒฐ์„ฑ์˜

๋ฌด๊ฒฐ์„ฑ์˜ customizing

  client < ------------ > dbms < ------------ > server

1. DDL๋ฌธ : ์„ ์–ธ์ ์ธ ๋ฐฉ๋ฒ• (table, view...)  ===> server

2. DML๋ฌธ : transaction + ํ”„๋กœ๊ทธ๋ž˜๋ฐ๋ฐฉ๋ฒ•

3. SP(Stored Procedure) : ํ”„๋กœ๊ทธ๋ž˜๋ฐ๋ฐฉ๋ฒ•, ์„ฑ๋Šฅ + ์ž๋™ X ===> server

4. trigger(๋ฐฉ์•„์‡ ) : ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ๋ฒ•, ์„ฑ๋Šฅ -, ์ž๋™ O  ===> server

5. index : ๊ฒ€์ƒ‰์„ฑ๋Šฅ +, ๊ฐฑ์‹ ์„ฑ๋Šฅ -  ===> server

6. DCL๋ฌธ

 

 

ํ”„๋กœ์‹œ์ € ๋žญ๊ธฐ์ง€(pl/sql)

 

 

์˜ˆ์‹œ ์ฝ”๋“œ

show user
--ํ™˜๊ฒฝ๋ณ€์ˆ˜ ํ™”๋ฉด์ถœ๋ ฅ์„ ํ•˜๋ ค๋ฉด on์ด ๋˜์–ด์•ผํ•จ
show serveroutput 
set serveroutput on 
--1. 1๋ถ€ํ„ฐ 10๊นŒ์ง€ ์ž์—ฐ์ˆ˜ ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•ด๋ผ
-- ์„ ์–ธ๋ฌธ
declare 
    v_num int;
    -- ํ• ๋‹น๋ฌธ ':='  ํŒŒ์Šค์นผ ๋žญ๊ธฐ์ง€์—์„œ ์œ ๋ž˜๋œ ๊ฒƒ์œผ๋กœ ์ดˆ๊ธฐ๊ฐ’์„ ์˜๋ฏธํ•œ๋‹ค.
    v_sum int:=0;
begin
    v_num:= 1;
    while v_num <=10
        loop
         v_sum:=v_sum+v_num;
         v_num:=v_num+1;
        end loop;   
        -- ์•„๋ž˜๋Š” ์˜ค๋ผํด์—์„œ ์ œ๊ณตํ•˜๋Š” ํŒจํ‚ค์ง€ ์ด๋ฆ„ - ๋ช‡๊ฐ€์ง€ ํ•จ์ˆ˜๊ฐ€ ์žˆ์Œ
    dbms_output.put_line(v_sum); --๋ฌธ์ž์—ด์€ ์“ธ ์ˆ˜ ์—†๋‚˜์š”?
--์ „์ฒด ์ค‘ ์—๋Ÿฌ๊ฐ€ ์ƒ๊ธด๋‹ค๋ฉด (exception)
exception
    when others then
        dbms_output.put_line(sqlcode||sqlerrm); --error code name
-- sqlerrm : ์ •์ƒ ์ข…๋ฃŒ
-- others : ๋น„์ •์ƒ ์ข…๋ฃŒ ์‹œํ‚ด
end;
/

 

์•„๋ž˜์™€ ๊ฐ™์ด if else ๋ฌธ์œผ๋กœ ์ œ์–ดํ•ด์„œ loop๋ฅผ ๊ฑธ์–ด์ค„ ์ˆ˜๋„ ์žˆ๋‹ค.

declare 
    v_num int;
    -- ํ• ๋‹น๋ฌธ ':='  ํŒŒ์Šค์นผ ๋žญ๊ธฐ์ง€์—์„œ ์œ ๋ž˜๋œ ๊ฒƒ์œผ๋กœ ์ดˆ๊ธฐ๊ฐ’์„ ์˜๋ฏธํ•œ๋‹ค.
    v_sum int:=0;
begin
    v_num:= 1;
        loop
         v_sum:=v_sum+v_num;
         if v_num >= 10 then
         	exit;
         -- elseif then
         else
         v_num:=v_num+1;
        end if;
        end loop;
        -- ์•„๋ž˜๋Š” ์˜ค๋ผํด์—์„œ ์ œ๊ณตํ•˜๋Š” ํŒจํ‚ค์ง€ ์ด๋ฆ„ - ๋ช‡๊ฐ€์ง€ ํ•จ์ˆ˜๊ฐ€ ์žˆ์Œ
    dbms_output.put_line(v_sum); --๋ฌธ์ž์—ด์€ ์“ธ ์ˆ˜ ์—†๋‚˜์š”?
--์ „์ฒด ์ค‘ ์—๋Ÿฌ๊ฐ€ ์ƒ๊ธด๋‹ค๋ฉด (exception)
exception
    when others then
        dbms_output.put_line(sqlcode||sqlerrm); --error code name
-- sqlerrm : ์ •์ƒ ์ข…๋ฃŒ
-- others : ๋น„์ •์ƒ ์ข…๋ฃŒ ์‹œํ‚ด
end;
/

-- sqlerrm : ์ •์ƒ ์ข…๋ฃŒ ์‹œํ‚ด (๋กค๋ฐฑ์„ ๋‚ด๊ฐ€ ์ •ํ•  ์ˆ˜ ์žˆ์Œ) 
-- others : ๋น„์ •์ƒ ์ข…๋ฃŒ ์‹œํ‚ด -> ํ•˜๋˜ ํ–‰์œ„๋ฅผ ๊ทธ๋ƒฅ ์ข…๋ฃŒ ์‹œํ‚ด 

 

 

์•„๋ž˜๋Š” 3์˜ ๋ฐฐ์ˆ˜๋งŒ ๋”ํ•˜๋Š” ์˜ˆ์ œ์ด๋‹ค.

declare 
    v_num int;
    -- ํ• ๋‹น๋ฌธ ':='  ํŒŒ์Šค์นผ ๋žญ๊ธฐ์ง€์—์„œ ์œ ๋ž˜๋œ ๊ฒƒ์œผ๋กœ ์ดˆ๊ธฐ๊ฐ’์„ ์˜๋ฏธํ•œ๋‹ค.
    v_sum int:=0;
begin
    v_num:= 3;
        loop
         if v_num >= 20 then
            
         	exit;
         -- elseif then
         else
         dbms_output.put_line(to_char(v_sum)||'+'||to_char(v_num));
         v_sum:=v_sum+v_num;
         v_num:=v_num+3;
        end if;
        end loop;
    dbms_output.put_line('total : '||to_char(v_sum));
    dbms_output.put_line('last number : '||to_char(v_num));
exception
    when others then
        dbms_output.put_line(sqlcode||sqlerrm); 
end;
/

์กฐ๊ธˆ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ์ ‘๊ทผ

declare 
    v_num int;
    -- ํ• ๋‹น๋ฌธ ':='  ํŒŒ์Šค์นผ ๋žญ๊ธฐ์ง€์—์„œ ์œ ๋ž˜๋œ ๊ฒƒ์œผ๋กœ ์ดˆ๊ธฐ๊ฐ’์„ ์˜๋ฏธํ•œ๋‹ค.
    v_sum int:=0;
begin
    v_num:= 1;
        loop
         if mod(v_num,3) = 0 then
         	v_sum:= v_sum+v_num;
         end if;
         if v_num >= 20 then
         	exit;
         -- elseif then
         else
         v_num:=v_num+1;
        end if;
        end loop;
    dbms_output.put_line('total : '||to_char(v_sum)||'last number :' 
     || to_char(v_num));
exception
    when others then
        dbms_output.put_line(sqlcode||sqlerrm); 
end;
/

์—๋Ÿฌ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•ด๋ณด๊ณ  ์‹ถ๋‹ค๋ฉด?

์—๋Ÿฌ ์œ ๋ฐœ -> 0์œผ๋กœ ๋‚˜๋ˆ ๋ด„ : ๋ถ€์ •(?) 

 

 

 

exception์ฒ˜๋ฆฌ๋„ ๋‹ค์–‘ํ•˜๊ฒŒ ์ปค์Šคํ„ฐ๋งˆ์ด์ง• ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

๊ฒฐ๊ณผ

 

 

์‚ฌ์šฉ์ž ์ •์˜ํ•จ์ˆ˜ ์˜ˆ์‹œ 1)

create or replace function get_dept_name(v_deptno dept.deptno%type)
return dept.dname%type
is 
    v_dname dept.dname%type;
begin
    select dname
    into v_dname
    from dept
    where deptno = v_deptno;
    return v_dname;
exception
    --when zero_divide then 
        --dbms_output.put_line('Try again! divider is 0!');
    when others then 
        dbms_output.put_line(sqlcode||' '||sqlerrm);
        return 0;
end;
/
--์œ„์—์„œ ํ–ˆ๋˜ ๋ฐฉ๋ฒ•์€ select๋ฌธ์—์„œ ํ•œ๊ฒƒ ์ž„.
--ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ๋ฒ•
--1. select ๋ฌธ์—์„œ!

select get_dept_name(10)
from dual;

select deptno,dname,get_dept_name(deptno)
from dept;

desc user_objects

select object_name, object_type
from user_objects
order by 2;

desc user_source
select line, text
from user_source
where name='GET_DEPT_NAME';


--2. ๋‹ค๋ฅธ ๋ชจ๋“ˆ์—์„œ!
declare
	v_dname varchar2(14);
begin
	--๊ฐ’์„ ๋ฆฌํ„ด ๋ฐ›์„ ๋ณ€์ˆ˜ ํ•˜๋‚˜๊ฐ€ ํ•„์š”ํ•จ.
	v_dname:=get_dept_name(30);
	dbms_output.put_line(v_dname);
end;
/

--3. sql*plus๋ช…๋ น! (pl/sql ์•„๋‹˜!)
var v_dname varchar2(14)
var 
exec :v_dname:= get_dept_name(30);
print v_dname 

 

์˜ˆ์‹œ 2) e

--empํ…Œ์ด๋ธ”์—์„œ ename์œผ๋กœ sal์„ ์ถœ๋ ฅํ•˜๋Š” function๋ช… f_Sal์„ ์ž‘์„ฑํ•˜๊ณ  ํ˜ธ์ถœํ•˜๋ผ.
create or replace function f_sal(v_ename emp.ename%type)
return emp.sal%type
is 
    v_sal emp.sal%type;
begin
    select sal
    into v_sal
    from emp
    where ename like '%'||v_ename||'%'; --์ •ํ™•์น˜ ์•Š์€ ๊ฒฝ์šฐ = ๋ง๊ณ  like๋ฅผ ์ค€๋‹ค.๊ทธ๋ž˜์„œ ์™€์ผ๋“œ ์นด๋“œ ๊ธฐํ˜ธ๋„ ์ค˜์•ผํ•จ
    return v_sal;
exception
    when others then 
        dbms_output.put_line(sqlcode||' '||sqlerrm);
        return 0;
end;
/

 

์ด์™ธ ๋ฌธ์ œ ํ’€์ด๋“ค..

 

 

create or replace function get_dept_name(v_deptno dept.deptno%type)
return dept.dname%type
is 
    v_dname dept.dname%type;
begin
    select dname
    into v_dname
    from dept
    where deptno = v_deptno;
    return v_dname;
exception
    --when zero_divide then 
        --dbms_output.put_line('Try again! divider is 0!');
    when others then 
        dbms_output.put_line(sqlcode||' '||sqlerrm);
        return 0;
end;
/
--์œ„์—์„œ ํ–ˆ๋˜ ๋ฐฉ๋ฒ•์€ select๋ฌธ์—์„œ ํ•œ๊ฒƒ ์ž„.
--ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ๋ฒ•
--1. select ๋ฌธ์—์„œ!

select get_dept_name(10)
from dual;

select deptno,dname,get_dept_name(deptno)
from dept;

desc user_objects

select object_name, object_type
from user_objects
order by 2;

desc user_source
select line, text
from user_source
where name='GET_DEPT_NAME';


--2. ๋‹ค๋ฅธ ๋ชจ๋“ˆ์—์„œ!
declare
	v_dname varchar2(14);
begin
	--๊ฐ’์„ ๋ฆฌํ„ด ๋ฐ›์„ ๋ณ€์ˆ˜ ํ•˜๋‚˜๊ฐ€ ํ•„์š”ํ•จ.
	v_dname:=get_dept_name(30);
	dbms_output.put_line(v_dname);
end;
/

--3. sql*plus๋ช…๋ น! (pl/sql ์•„๋‹˜!)
var v_dname varchar2(14)
var 
exec :v_dname:= get_dept_name(30);
print v_dname 


--empํ…Œ์ด๋ธ”์—์„œ ename์œผ๋กœ sal์„ ์ถœ๋ ฅํ•˜๋Š” function๋ช… f_Sal์„ ์ž‘์„ฑํ•˜๊ณ  ํ˜ธ์ถœํ•˜๋ผ.
create or replace function f_sal(v_ename emp.ename%type)
return emp.sal%type
is 
    v_sal emp.sal%type;
begin
    select sal
    into v_sal
    from emp
    where ename like '%'||v_ename||'%'; --์ •ํ™•์น˜ ์•Š์€ ๊ฒฝ์šฐ = ๋ง๊ณ  like๋ฅผ ์ค€๋‹ค.๊ทธ๋ž˜์„œ ์™€์ผ๋“œ ์นด๋“œ ๊ธฐํ˜ธ๋„ ์ค˜์•ผํ•จ
    return v_sal;
exception
    when TOO_MANY_ROWS then --์ •ํ•ด์ง„ ์—๋Ÿฌ๋ช…์ž„ :ํ•ด๋‹น ์—๋Ÿฌ๊ฐ€ ์ƒ๊ธฐ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ถœ๋ ฅํ•˜๋ผ๋Š” ๊ฒƒ์ž„.
    dbms_output.put_line('Select only one row!');
        return 0;
    when others then 
        dbms_output.put_line(sqlcode||' '||sqlerrm);
        return 0;
end;
/

select f_sal('SMITH')
from emp;

select ename,sal,f_sal(ename)
from emp;

select f_sal('S')
from dual;

declare
    v_sal number(10);
begin
    v_sal:= f_sal('SMI');
    dbms_output.put_line(v_sal);
end;
/
--select๋ฌธ์œผ๋กœ ์ถœ๋ ฅ
select f_sal('SMI')
from dual;
--๋˜๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ์ถœ๋ ฅ
var x_sal number
var 
exec :x_sal:=f_sal('SMI');
print x_sal


--4๋ฒˆ ๋ฌธ์ œ์ž„
create or replace function f_sal(v_ename emp.ename%type)
return emp.sal%type
is 
    v_sal emp.sal%type;
    v_job emp.job%type;
begin
    select sal,job
    into v_sal,v_job
    from emp
    where ename like '%' ||v_ename||'%';
    if v_job='MANAGER' then
        v_sal:=v_sal+v_sal*0.05;
    elsif v_job ='SALESMAN' then
        v_sal:= v_sal+v_sal*0.1;
    end if;
    return v_sal;
exception
    when TOO_MANY_ROWS then --์ •ํ•ด์ง„ ์—๋Ÿฌ๋ช…์ž„ :ํ•ด๋‹น ์—๋Ÿฌ๊ฐ€ ์ƒ๊ธฐ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ถœ๋ ฅํ•˜๋ผ๋Š” ๊ฒƒ์ž„.
    dbms_output.put_line('Try again!');
        return 0;
    when others then 
        dbms_output.put_line(sqlcode||' '||sqlerrm);
        return 0;
end;
/
--select ๊ฒฐ๊ณผ
select ename,job,sal,f_sal(ename)
from emp;

select * from salgrade;
--5๋ฒˆ ๋ฌธ์ œ : salgradeํ…Œ์ด๋ธ”์—์„œ grade๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ํ•ด๋‹น grade์˜ ์ค‘๊ฐ„ ๊ฐ’ sal์ด ๋ฆฌํ„ด ๋˜๋„๋ก
--f_midsalํ•จ์ˆ˜ ์ž‘์„ฑํ•˜๊ณ  ํ˜ธ์ถœํ•˜๋ผ
create or replace function f_midsal(v_grade salgrade.grade%type)
return salgrade.losal%type
is 
    v_midsal salgrade.losal%type; --losal๊ณผ hisal์€ ํƒ€์ž…์ด ๊ฐ™์œผ๋ฏ€๋กœ ๋ฌด์—‡์„ ์‚ฌ์šฉํ•˜๋˜์ง€, ๋ฌด๊ด€ํ•˜๋‹ค.

begin
    select round((losal+hisal)/2)
    into v_midsal
    from salgrade
    where grade = v_grade;
    return v_midsal;
exception
    when others then 
        dbms_output.put_line(sqlcode||sqlerrm);
        return 0;
end;
/
select grade,losal,hisal,f_midsal(grade) as midsal
from salgrade;

--ํ”„๋กœ์‹œ์ €๋กœ ์œ„ ํ•จ์ˆ˜๋ฅผ ๋ฐ”๊ฟ”๋ณด๊ธฐ object ๋ช…๊ณผ, type์„ ๋ฐ”๊ฟ”์คŒ
--c์–ธ์–ด์˜ ๋งˆ์น˜ void์ฒ˜๋Ÿผ ! return type์ด ์—†๋‹ค.
create or replace procedure p_midsal(v_grade salgrade.grade%type)
is 
    v_midsal salgrade.losal%type;
begin
    select round((losal+hisal)/2)
    into v_midsal
    from salgrade
    where grade = v_grade;
    dbms_output.put_line(v_midsal);
exception
    when others then 
        dbms_output.put_line(sqlcode||sqlerrm);
end;
/
-- select๋กœ ํ”„๋กœ์‹œ์ € ํ˜ธ์ถœ ํ•  ์ˆ˜ ์—†์Œ.
-- exec๋กœ ์ถœ๋ ฅ ๊ฐ€๋Šฅ(3๋ฒˆ์งธ ๋ฐฉ๋ฒ•) sql+์—์„œ ๋ฐฉ๋ฒ•์ค‘ ์—‘์‹œํํŠธ(exec)๋กœ ์ถœ๋ ฅ!
exec p_midsal(2);

begin 
    p_midsal(2);
end;
/

 

๋งŒ์•ฝ ํ™”๋ฉด ์ถœ๋ ฅ &ํ”„๋ฆฐํŠธ๊ฐ€ ์•ˆ๋˜๋Š” ๊ฒฝ์šฐ -> 

show serveroutput 
set serveroutput on

๊ผญ ํ•ด์ฃผ์–ด์•ผ ํ•ด๊ฒฐ์ด ๋จ

 

 ์ตœ์ข…(์ •๋ฆฌํ•ด๋†“๊ธฐ)

create or replace function get_dept_name(v_deptno dept.deptno%type)
return dept.dname%type
is 
    v_dname dept.dname%type;
begin
    select dname
    into v_dname
    from dept
    where deptno = v_deptno;
    return v_dname;
exception
    --when zero_divide then 
        --dbms_output.put_line('Try again! divider is 0!');
    when others then 
        dbms_output.put_line(sqlcode||' '||sqlerrm);
        return 0;
end;
/
--์œ„์—์„œ ํ–ˆ๋˜ ๋ฐฉ๋ฒ•์€ select๋ฌธ์—์„œ ํ•œ๊ฒƒ ์ž„.
--ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ๋ฒ•
--1. select ๋ฌธ์—์„œ!

select get_dept_name(10)
from dual;

select deptno,dname,get_dept_name(deptno)
from dept;

desc user_objects

select object_name, object_type
from user_objects
order by 2;

desc user_source
select line, text
from user_source
where name='GET_DEPT_NAME';


--2. ๋‹ค๋ฅธ ๋ชจ๋“ˆ์—์„œ!
declare
	v_dname varchar2(14);
begin
	--๊ฐ’์„ ๋ฆฌํ„ด ๋ฐ›์„ ๋ณ€์ˆ˜ ํ•˜๋‚˜๊ฐ€ ํ•„์š”ํ•จ.
	v_dname:=get_dept_name(30);
	dbms_output.put_line(v_dname);
end;
/

--3. sql*plus๋ช…๋ น! (pl/sql ์•„๋‹˜!)
var v_dname varchar2(14)
var 
exec :v_dname:= get_dept_name(30);
print v_dname 


--empํ…Œ์ด๋ธ”์—์„œ ename์œผ๋กœ sal์„ ์ถœ๋ ฅํ•˜๋Š” function๋ช… f_Sal์„ ์ž‘์„ฑํ•˜๊ณ  ํ˜ธ์ถœํ•˜๋ผ.
create or replace function f_sal(v_ename emp.ename%type)
return emp.sal%type
is 
    v_sal emp.sal%type;
begin
    select sal
    into v_sal
    from emp
    where ename like '%'||v_ename||'%'; --์ •ํ™•์น˜ ์•Š์€ ๊ฒฝ์šฐ = ๋ง๊ณ  like๋ฅผ ์ค€๋‹ค.๊ทธ๋ž˜์„œ ์™€์ผ๋“œ ์นด๋“œ ๊ธฐํ˜ธ๋„ ์ค˜์•ผํ•จ
    return v_sal;
exception
    when TOO_MANY_ROWS then --์ •ํ•ด์ง„ ์—๋Ÿฌ๋ช…์ž„ :ํ•ด๋‹น ์—๋Ÿฌ๊ฐ€ ์ƒ๊ธฐ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ถœ๋ ฅํ•˜๋ผ๋Š” ๊ฒƒ์ž„.
    dbms_output.put_line('Select only one row!');
        return 0;
    when others then 
        dbms_output.put_line(sqlcode||' '||sqlerrm);
        return 0;
end;
/

select f_sal('SMITH')
from emp;

select ename,sal,f_sal(ename)
from emp;

select f_sal('S')
from dual;

declare
    v_sal number(10);
begin
    v_sal:= f_sal('SMI');
    dbms_output.put_line(v_sal);
end;
/
--select๋ฌธ์œผ๋กœ ์ถœ๋ ฅ
select f_sal('SMI')
from dual;
--๋˜๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ์ถœ๋ ฅ
var x_sal number
var 
exec :x_sal:=f_sal('SMI');
print x_sal


--4๋ฒˆ ๋ฌธ์ œ์ž„
create or replace function f_sal(v_ename emp.ename%type)
return emp.sal%type
is 
    v_sal emp.sal%type;
    v_job emp.job%type;
begin
    select sal,job
    into v_sal,v_job
    from emp
    where ename like '%' ||v_ename||'%';
    if v_job='MANAGER' then
        v_sal:=v_sal+v_sal*0.05;
    elsif v_job ='SALESMAN' then
        v_sal:= v_sal+v_sal*0.1;
    end if;
    return v_sal;
exception
    when TOO_MANY_ROWS then --์ •ํ•ด์ง„ ์—๋Ÿฌ๋ช…์ž„ :ํ•ด๋‹น ์—๋Ÿฌ๊ฐ€ ์ƒ๊ธฐ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ถœ๋ ฅํ•˜๋ผ๋Š” ๊ฒƒ์ž„.
    dbms_output.put_line('Try again!');
        return 0;
    when others then 
        dbms_output.put_line(sqlcode||' '||sqlerrm);
        return 0;
end;
/
--select ๊ฒฐ๊ณผ
select ename,job,sal,f_sal(ename)
from emp;

select * from salgrade;
--5๋ฒˆ ๋ฌธ์ œ : salgradeํ…Œ์ด๋ธ”์—์„œ grade๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ํ•ด๋‹น grade์˜ ์ค‘๊ฐ„ ๊ฐ’ sal์ด ๋ฆฌํ„ด ๋˜๋„๋ก
--f_midsalํ•จ์ˆ˜ ์ž‘์„ฑํ•˜๊ณ  ํ˜ธ์ถœํ•˜๋ผ
create or replace function f_midsal(v_grade salgrade.grade%type)
return salgrade.losal%type
is 
    v_midsal salgrade.losal%type; --losal๊ณผ hisal์€ ํƒ€์ž…์ด ๊ฐ™์œผ๋ฏ€๋กœ ๋ฌด์—‡์„ ์‚ฌ์šฉํ•˜๋˜์ง€, ๋ฌด๊ด€ํ•˜๋‹ค.

begin
    select round((losal+hisal)/2)
    into v_midsal
    from salgrade
    where grade = v_grade;
    return v_midsal;
exception
    when others then 
        dbms_output.put_line(sqlcode||sqlerrm);
        return 0;
end;
/
select grade,losal,hisal,f_midsal(grade) as midsal
from salgrade;

--ํ”„๋กœ์‹œ์ €๋กœ ์œ„ ํ•จ์ˆ˜๋ฅผ ๋ฐ”๊ฟ”๋ณด๊ธฐ object ๋ช…๊ณผ, type์„ ๋ฐ”๊ฟ”์คŒ
--c์–ธ์–ด์˜ ๋งˆ์น˜ void์ฒ˜๋Ÿผ ! return type์ด ์—†๋‹ค.
create or replace procedure p_midsal(v_grade salgrade.grade%type)
is 
    v_midsal salgrade.losal%type;
begin
    select round((losal+hisal)/2)
    into v_midsal
    from salgrade
    where grade = v_grade;
    dbms_output.put_line(v_midsal);
exception
    when others then 
        dbms_output.put_line(sqlcode||sqlerrm);
end;
/
-- select๋กœ ํ”„๋กœ์‹œ์ € ํ˜ธ์ถœ ํ•  ์ˆ˜ ์—†์Œ.
-- exec๋กœ ์ถœ๋ ฅ ๊ฐ€๋Šฅ(3๋ฒˆ์งธ ๋ฐฉ๋ฒ•) sql+์—์„œ ๋ฐฉ๋ฒ•์ค‘ ์—‘์‹œํํŠธ(exec)๋กœ ์ถœ๋ ฅ!
exec p_midsal(2);

begin 
    p_midsal(2);
end;
/
--test table์ƒ์„ฑ
drop table testemp;
create table testemp
as select empno,ename,sal,deptno from emp;
select * from testemp;
alter table testemp
add grade number(1);
alter table testemp
add sumsal number(10);

update testemp t;
/*์˜ˆ์‹œ
set sumsal = (select sum(sal)
                from testemp
                where t.deptno=deptno );
*/     
/
create or replace procedure p_sum01(v_deptno testemp.deptno%type)
is 
    v_sumsal testemp.sumsal%type;
begin
    select sum(sal)
    into v_sumsal
    from testemp
    where deptno=v_deptno;
    if v_deptno=10 then
        v_sumsal:=0;
    end if;
    if v_sumsal >= 10000 then
        dbms_output.put_line('10000 over!');
    end if;
    update testemp 
    set sumsal = v_sumsal
    where deptno = v_deptno;
    --commit์€ db์— ๋ฐ˜์˜ํ•˜๊ฒ ๋‹ค๋Š” ๋œป์ด๋‹ค.
    commit;
exception
    when others then
    dbms_output.put_line(sqlcode);
    rollback;
end;
/

exec p_sum01(30);
select * from testemp;
commit;-- ๋ณ€๊ฒฝ์„ ์ €์žฅํ•˜๋ ค๋ฉด ์ปค๋ฐ‹ํ•ด์ค˜์•ผํ•จ. ์•ˆํ•˜๊ณ  ๋ฐ”๋กœ ์ปดํŒŒ์ผ ์‹œ ๋กค๋ฐฑ ๋‹นํ•จ.

--insert๋ฌธ์— update๊ฐ€ ๊ผญ ๋”ฐ๋ผ๋‹ค๋…€์•ผ ํ•œ๋‹ค.
create or replace procedure p_insert01(v_empno testemp.empno%type,
                                       v_ename testemp.ename%type,
                                       v_sal testemp.sal%type,
                                       v_deptno testemp.deptno%type)
is 
begin
    insert into testemp
    values(v_empno,v_ename,v_sal,v_deptno,null,null);
    update testemp
    set sumsal=(select sum(sal)
                from testemp
                where deptno=v_deptno)
    where deptno=v_deptno;
    commit;
exception
    when others then
        dbms_output.put_line(sqlcode||sqlerrm);
        rollback;
end;
/
select * from testemp;
exec p_insert01(9998,'kim',2000,20);


-- 7-1๋ฒˆ ๋ฌธ์ œ์ž„ ์œ„๋Š” 7๋ฒˆ์ด๊ณ , ์ด๋ฅผ ํ•ฉํ•ด์„œ ๋งŒ๋“  ๊ฒƒ!
create or replace procedure p_insert02(v_empno testemp.empno%type,
                                       v_ename testemp.ename%type,
                                       v_sal testemp.sal%type,
                                       v_deptno testemp.deptno%type)
is 
    v_sumsal testemp.sumsal%type;
begin
    insert into testemp
    values(v_empno,v_ename,v_sal,v_deptno,null,null);
    update testemp
    set sumsal=(select sum(sal)
                from testemp
                where deptno=v_deptno)
    where deptno=v_deptno;
    select sum(sal)
    into v_sumsal
    from testemp
    where deptno=v_deptno;
    if v_deptno=10 then
        v_sumsal:=0;
    end if;
    if v_sumsal >= 10000 then
        dbms_output.put_line('10000 over!');
    end if;
    commit;
exception
    when others then
        dbms_output.put_line(sqlcode||sqlerrm);
        rollback;
end;
/
select * from testemp where ;
exec p_insert02(9999,'kim',2000,20);


๋ฐ˜์‘ํ˜•
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.