저장 프로 세 스 와 저장 함수 와 트리거 예제

1. 저장 과정 예시: 지 정 된 직원 에 게 원래 의 임금 을 바탕 으로 10% 의 월급 을 준다.
SQL> create or replace procedure raiseSalary(empid in number)
 
    as
 
    pSal emp.sal%type;
 
    begin
 
    select sal into pSal from emp where empno=empid;
 
    update emp set sal = sal*1.1 where empno=empid;
 
    dbms_output.put_line('   :' || empid || '    
 
    ' || psal || '    ' || psal*1.1);
 
    end;
 
   /
 
Procedure created
 
SQL> set serveroutput on
 
SQL> exec raisesalary(7369);

직원 번호: 7369 임금 인상 전
 
800 임금 인상 후 880
 
PL/SQL procedure successfully completed
 
2. 저장 함수 예시: 모 직원 의 연수 입 을 조회 합 니 다.
SQL> /**   모 직공 의 총수입 을 조회 하 다   */
create or replace function queryEmpSalary(empid in number)
 
    return number
 
    as
 
    pSal number; --           
 
    pComm number; --           
 
   begin
 
   select sal,comm into psal,pcomm from emp where empno = empid;
 
   return psal*12+nvl(pcomm,0);
 
   end;
 
   /
 
Function created
 
SQL> declare
 
    v_sal number;
 
    begin
 
    v_sal:=queryEmpSalary(7934);
 
    dbms_output.put_line('salary is:'|| v_sal);
 
    end;
 
    /
 
salary is:15600
 
PL/SQL procedure successfully completed
 
SQL> begin
 
    dbms_output.put_line('salary is:'|| queryEmpSalary(7934));
 
    end;
 
    /
 
salary is:15600
 
PL/SQL procedure successfully completed

 
3. 트리거 예제 생 성 1: 비 작업 시간 을 제한 하여 데이터베이스 에 데 이 터 를 삽입 합 니 다.
SQL> create or replace
 
    trigger securityEmp
 
    before insert on emp
 
    declare
 
    begin
 
    if to_char(sysdate,'day')in('   ','   ','   ')
 
    or to_number(to_char(sysdate,'hh24'))not between 8 and 18 then 
 
    raise_application_error(-20001,'');
 
    end if;
 
   end;
 
   /
 
Trigger created

 
4. 트리거 예제 2: 데 이 터 를 확인 합 니 다 (emp 표 에서 sal 의 수정 값 이 원래 값 보다 낮 지 않 음 을 검사 합 니 다)
SQL> create or replace trigger checkSal
 
    before update of sal on emp
 
    for each row
 
    declare
 
    begin
 
    if :new.sal<:old.sal then
 
    raise_application_error(-20001,'           ');
 
    end if;
 
    end;
 
   /
 
Trigger created

 

좋은 웹페이지 즐겨찾기