Chinaunix首页 | 论坛 | 博客
  • 博客访问: 51406
  • 博文数量: 23
  • 博客积分: 1415
  • 博客等级: 上尉
  • 技术积分: 281
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-04 16:00
文章分类

全部博文(23)

文章存档

2014年(19)

2008年(1)

2007年(3)

我的朋友

分类: Oracle

2014-05-23 16:58:58

                         
一、应用背景:
      一般PL/SQL块,当执行完成后,就消失了;下次执行时,还需要再次编写。因此人们希望能够将PL/SQL块存储在数据
  库中,并且可以多次执行,在这种需求的驱动下出现了有名PL/SQL块(也叫做非匿名块)。有名PL/SQL块分为:存储过程、
  函数、触发器。本文详细介绍存储过程的使用。

二、存储过程(procedure):
   
       存储过程(procedure)是一个有名PL/SQL块。它的本质是PL/SQL块,只是给这个块起了一个名字,并且存储在数据库
   中,可以通过名字多次执行。

       分类:无参存储过程、有参存储过程

       注意事项:在过程中不能有declare,所有变量定义在begin之前。
      
三、存储过程的定义:

  语法:

      1、无参数的存储过程
       
        create or replace procedure procedure_name
        is
          ...           //Here, it is definition variable
        begin
          ...
        exception
          ...
        end;


      2、带参数的存储过程


        create or replace procedure procedure_name(para1 in|out data_type,...)
        is 
          ...        //Here, it is definition variable
        begin
          ...
        exception
          ...
        end;


        //带参数的存储过程,参数在过程体中无需再定义
              
  说明:
       create or replace procedure, is  ----是关键字
       procedure_name                   ----表示过程名字
       para1                            ----表示过程的形参,形参可以是输入参数,也可以是输出参数,或者兼二有之。
       in                               ----表示参数是输入参数
       out                              ----表示参数是输出参数


四、存储过程使用:
  
      1、使用命令:

        exec procedure_name;                --例如:exec p_emp;
        exec procedure_name(para1,para2);   --例如:exec p_area(5,4);          

      2、使用PL/SQL块调用:
         
        begin
          procedure_name;
          procedure_name(para1,para2);         
        end;
  
        例如:
        begin
          p_emp;
          --p_area(5,4);
        end;
      

五、实例应用


   1、无参存储过程的使用----遍历表数据: 
    
    --创建存储过程
    
    --set serverout on;         打开服务器输出。

    create or replace procedure p_course
    is
      cursor c is select * from c;
    begin
      for v_c in c                          --for循环中游标不需要open、close、featch,for语句会自动完成。
      loop 
       dbms_output.put_line('The name of course is  '||v_c.cname);
      end loop;
    end;


    --使用存储过程 
  
    exec p_course;
 
    执行结果:
     
    SQL> exec p_course;
    The name of course is  chinese
    The name of course is  math
    The name of course is  english
    The name of course is  politicle
    The name of course is  physical
    The name of course is  china hisotry


    PL/SQL procedure successfully completed.




   
  2、有参存储过程的使用------求长方形的面积: 


    --创建存储过程


    --set serverout on;      打开服务器输出。


    create or replace procedure p_area(v_length in number,v_width in number)
    is
      v_area number;
    begin
      v_area := v_length * v_width;
      dbms_output.put_line('The area is :'||v_area);
    end;
    
    --使用存储过程


    exec p_area(5,4); 


    
    执行结果:
 
    The area is :20


    PL/SQL procedure successfully completed.




  3、有参存储过程的使用---求长方体体积: 
 
    --set serverout on;      打开服务器输出。
 
    --创建存储过程
    create or replace procedure p_volume(v_len number,v_wid number,v_high number,v_vol in out number)
    is
    begin
      v_vol := v_len * v_wid * v_high;
    end;
    
    --使用存储过程


    declare
      v_len number:=5;
      v_wid number:=4;
      v_high number:=5;
      v_vol number;
    begin
      p_volume(v_len,v_wid,v_high,v_vol);               --将四个变量传递给过程的形参。
      dbms_output.put_line('The volume is '||v_vol);
    end;


    执行结果:
 
    The volum is :100


    PL/SQL procedure successfully completed.
  
   4、无参存储过程的使用---更新表数据:
 
    --创建存储过程
     
    --set serverout on;      打开服务器输出。
 
    create or replace procedure p_update_emp_sal
    is
      cursor c is select * from emp for update;
    begin
      for c_emp in c 
      loop
        if (c_emp.sal < 2000) then 
          update emp set sal=sal + 5000 where current of c;
        elsif (c_emp.sal between 2000 and 10000) then
          update emp set sal=sal + 3000 where current of c;
        elsif (c_emp.sal between 10000 and 20000 ) then
          update emp set sal=sal + 500  where current of c;
        else
          update emp set sal=sal + 100  where current of c; 
        end if;
       end loop;
       commit;
     end;


     --使用存储过程:


     exec p_update_emp_sal;


     --执行结果:


     SQL> exec p_update_emp_sal;


     PL/SQL procedure successfully completed.

阅读(389) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~