zoukankan      html  css  js  c++  java
  • Oracle入门基础(十二)一一储存过程及触发器

    1.第一个存储过程

    打印Hello World

    调用存储过程:

    1.exec sayhelloworld();
    2.begin
    			sayhelloworld();
    			sayhelloworld();
    		end;
    		
    	create or replace procedure sayhelloworld
    	as
    	   --说明部分
    	begin
    	   dbms_output.put_line('Hello World');
    end;
    

    2.带参数的存储过程

    给指定的员工涨100,并且打印涨前和涨后的薪水

    create or replace procedure raiseSalary(eno in number)
    is
           --定义变量保存涨前的薪水
           psal emp.sal%type;
    begin
           --得到涨前的薪水
           select sal into psal from emp where empno=eno;
           --涨100
           update emp set sal=sal+100 where empno=eno;
           --要不要commit?
           dbms_output.put_line('涨前:'||psal||'   涨后:'||(psal+100));
    end raiseSalary;
    

    3.存储函数

    查询某个员工的年收入

    create or replace function queryEmpIncome(eno in number) 
    return number
    is
           --定义变量保存月薪和奖金
           psal emp.sal%type;
           pcomm emp.comm%type;
    begin
           --得到月薪和奖金
           select sal,comm into psal,pcomm from emp where empno=eno; 
       
       --返回年收入
       return psal*12+nvl(pcomm,0);
    
    end queryEmpIncome;
    

    4.out参数

    –查询某个员工的姓名 薪水和职位

    /*
    1、查询某个员工的所有信息 —> out参数太多
    2、查询某个部门中的所有员工信息 ----> 返回的是集合
    */

    create or replace procedure queryEmpInformation(eno in number,
                                                    pename out varchar2,
                                                    psal   out number,
                                                    pjob   out varchar2)
    is
    begin
      
       select ename,sal,job into pename,psal,pjob from emp where empno=eno;                                             
    
    end queryEmpInformation;
    

    5.在out参数中使用光标

    查询某个部门中的所有员工信息 ----> 返回的是集合

    create or replace package mypackage is
    
       type empcursor is ref cursor;
       procedure queryEmpList(dno in number,empList out empcursor);
    
    end mypackage;
    
    create or replace package body mypackage is
    
       procedure queryEmpList(dno in number,empList out empcursor)
       as
       begin
         
          open empList for select * from emp where deptno=dno;
       
       end;
    
    end mypackage;
    

    6.第一个触发器

    每当成功插入新员工后,自动打印“成功插入新员工”

    create trigger firsttrigger
    after insert
    on emp
    declare
    begin
      dbms_output.put_line('成功插入新员工');
    end;
    

    触发器应用一

    实施复杂的安全性检查
    禁止在非工作时间 插入新员工

    1、周末: to_char(sysdate,‘day’) in (‘星期六’,‘星期日’)
    2、上班前 下班后:to_number(to_char(sysdate,‘hh24’)) not between 9 and 17

    create or replace trigger securityemp
    before insert
    on emp
    begin
       if to_char(sysdate,'day') in ('星期六','星期日','星期五') or 
          to_number(to_char(sysdate,'hh24')) not between 9 and 17 then
          --禁止insert
          raise_application_error(-20001,'禁止在非工作时间插入新员工');
       end if;
    end securityemp;
    

    触发器应用二

    /*
    数据的确认
    涨后的薪水不能少于涨前的薪水

    create or replace trigger checksalary
    before update
    on emp
    for each row
    begin
        --if 涨后的薪水 < 涨前的薪水 then
        if :new.sal < :old.sal then
           raise_application_error(-20002,'涨后的薪水不能少于涨前的薪水。涨前:'||:old.sal||'   涨后:'||:new.sal);
    end if;
      end checksalary;
    
  • 相关阅读:
    Java Socket
    路由器和交换机的区别
    OSI七层协议
    traceroute命令
    DNS递归和迭代原理
    出栈入栈顺序问题
    A记录、CNAME记录、MX记录
    DNS解析原理
    RAID磁盘阵列0、1、5、10
    http状态码
  • 原文地址:https://www.cnblogs.com/Aaron-007/p/12814619.html
Copyright © 2011-2022 走看看