存储过程

来源:互联网 发布:收割机webshell扫描器 编辑:程序博客网 时间:2024/06/06 21:38
1、定义:存储过程、存储函数是指在存储在数据库中供所有用户程序调用的子程序
2、语法:
     create or replace procedure 过程名
     as 说明部分
     begin
        PLSQL子程序体
     end
3、例子:
     create or replace procedure sayhelloworld
     as
     begin
       dbms_output.put_line("helloworld");
     end
4、调用存储过程的两种方式:
   <1>exec sayhelloworld();
   <2>begin
              sayhelloworld();
           end
5、带参数的存储过程
    例子:给指定的员工涨100块钱的工资,并打印涨前和涨后的工资
     create or replace procedure raisesalary(eno in number)
     as
       psal emp.sal %type;  --定义一个变量保存涨前的薪水
     begin
       select sal into psal from emp where empno=eno;  --得到员工涨前的薪水
       update emp set sal=sal+100 where empno=eno;   --给员工涨100块钱的薪水,在这里,我们一般不commit和rollback,而在调用的时候进行事务的提交和回滚
       dbms_output.put_line("涨前:"||psal||"涨后:"||(psal+100));  --打印涨前涨后的薪水
     end
6、存储过程的调试

7、存储函数
   create or replace function 函数名(参数列表)
   return 函数值类型
   as 
   PLSQL子程序体
 8、 例子:查询某个员工的年收入
    create or replace function queryempincome(eno in number)
    return number
    as
       psal emp.sal%type;  --定义变量保存员工的薪水
       pcomm emp.comm%type;   --定义变量保存员工的奖金
    begin
       select sal,comm into psal,pcomm from emp where empno=eno;  --得到该员工的月薪和奖金
    return sal*12+pcomm;   --直接返回年收入
    end
9、in和out参数
    存储过程和存储函数的区别是是否有返回值,但他们都可以通过out指定一个或多个输出参数,我们可以利用out参数,在过程和函数中实现返回多个值
10、例子:查询某个员工的姓名、月薪和职位
     原则:如果只存在一个返回值,用存储函数,否则,用存储过程
     create or replace procedure queryempinform(eno in number,
                                                                                  pname out varchar2,
                                                                                  psal out number,
                                                                                  pjob out varchar2)
      as
      begin
          select ename,sal,empjob into pename,psal,pjob from emp where empno=eno;
      end
   
0 0
原创粉丝点击