7월 3일 수요일(2) 실습
PL/SQL 실습
1)
CREATE OR REPLACE Procedure p_DEPT_insert
2 (
3 pl_DEPTNO in number,
4 pl_dname in varchar2,
5 pl_loc in varchar2,
6 pl_result out varchar2
7 )
8 IS
9 cnt number := 0;
10 BEGIN
11 SELECT COUNT(*) INTO cnt
12 FROM DEPT
13 WHERE DEPTNO = pl_DEPTNO;
14
15 if cnt > 0 then
16 pl_result := 'exist!!';
17 else
18 INSERT INTO DEPT (DEPTNO, DNAME, LOC)
19 VALUES (pl_DEPTNO, pl_dname, pl_loc);
20 COMMIT;
21 pl_result := 'input OK!!';
22 end if;
23
24 EXCEPTION
25 WHEN OTHERS THEN
26 ROLLBACK;
27 pl_result := 'error!!';
28 END;
29 /
Procedure created.
SQL> variable test1 varchar2(40);
SQL> EXECUTE p_DEPT_insert(10,'RESEARCH','seoul', :test1);
PL/SQL procedure successfully completed.
SQL> print test1;
TEST1
--------------------------------------------------------------------------------
exist!!
SQL> EXECUTE p_DEPT_insert(50,'RESEARCH','seoul', :test1);
PL/SQL procedure successfully completed.
SQL> SELECT * FROM DEPT;
DEPTNO DNAME LOC
---------- ---------------------------- --------------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 RESEARCH seoul
2) REM --2022.05.11 SH. 10X
declare
2 i number(3) := 1;
3 j number(3) := 1;
4 begin
5 for i in 1..10 loop
6 for j in 1..10 loop
7 dbms_output.put('#');
8 end loop;
9 dbms_output.put_line('@ ');
10 end loop;
11 end;
12 /
##########@
##########@
##########@
##########@
##########@
##########@
##########@
##########@
##########@
##########@
PL/SQL procedure successfully completed.
4. 특정 부서의 사원 목록 출력
SQL> declare
cursor c_emp is
select ename, job from emp where deptno = 20;
v_ename emp.ename%type;
v_job emp.job%type;
begin
open c_emp;
loop
fetch c_emp into v_ename, v_job;
exit when c_emp%notfound;
dbms_output.put_line('name: ' || v_ename
|| ' ; job: ' || v_job);
end loop;
close c_emp;
end;
/
5. 모든 사원의 연봉 10% 인상
begin
update emp set sal = sal * 1.1;
commit;
end;
/