PLSQL存储过程

作者: _借东西的小人 | 来源:发表于2017-08-23 18:32 被阅读28次

    存储过程

    关键字:procedure

    存储过程是PLSQL的一个方面的应用,而PLSQL是存储过程的基础。
    即存储过程需要用到PLSQL

    创建无参存储过程hello,无返回值
    语法:

    create or replace procedure
     过程名 as PLSQL程序
    
    set serveroutput on;
    create or replace procedure hello
    as
    begin
      dbms_output.put_line('这是我的第一个存储过程');
    end;
    

    删除存储hello,语法drop procedure 过程名

    drop procedure hello;
    

    调用存储过程方式一:exec 过程名
    exec hello;
    调用过程存储方式二:PLSQL程序

    begin
      hello;
    end;
    

    调用过程存储方式三:Java程序CallableStatement接口
    创建有存储过程的raiseSalary(编号),为7369号员工涨工资10%,演示in的语法,大小写不敏感

    --定义过程
    create or replace procedure raiseSalary(p_empno in number)
    as
    begin
      update emp set sal=sal*1.1 where empno=p_empno;
    end;
    --调用过程
    exec raiseSalary(7934);
    select * from emp where empno=7934;
    

    创建有参存储过程findEmpNameAndSalAndJob(编号),查询7788号员工的姓名,职位,月薪,返回多个值,演示out的用法

    --定义过程
    create or replace procedure findEmpNameAndSalAndJob(p_empno in number,p_name out varchar2,
    p_job out varchar2,p_sal out number)
    as
    begin
      select ename,job,sal into p_name,p_job,p_sal from emp where empno=7499;
    end;
    --调用过程
    declare
      p_name emp.ename%type;
      p_job emp.job%type;
      p_sal emp.sal%type;
    begin
      findEmpNameAndSalAndJob(7499,p_name,p_job,p_sal);
      dbms_output.put_line('7734号员工'||p_name||'----'||p_job||'----'||p_sal);
    end;
    
    注意: exce 适用于过程无返回值, plsql 调用适用于过程有返回值,不管多少个。

    用存储过程,写一个计算个人所得税的功能

    --定义过程
    create or replace procedure get_rax(sal in number,rax out number)
    as
     --sal表示收入
     --dal表示需要缴税的收入
     bal number;
    begin
     bal:=sal-3500;
     if bal<=1500 then
       rax:=bal*0.03-0;
     elsif bal<4500 then
       rax:=bal*0.1-105;
     elsif bal<9000 then
       rax:=bal*0.2-555;
     elsif bal<35000 then
       rax:=bal*0.3-2775;
     elsif bal<80000 then
       rax:=bal*0.45-5505;
     else
       rax:=bal*0.45-13505;
     end if;
    end;
    --调用过程
    declare
     rax number;
    begin
     get_rax(&sal,rax);
     dbms_output.put_line('您需要交的税'||rax);
    end;
    

    相关文章

      网友评论

        本文标题:PLSQL存储过程

        本文链接:https://www.haomeiwen.com/subject/ntdjdxtx.html