Oracle数据库存储过程

一:说明

  之前的匿名代码块不能重复使用,只能编译和运行一次,如果想复用匿名代码块,我们需要存储过程,函数,触发器等。

二:概念

  存储过程就是封装了一个plsql的匿名代码块,可以通过参数进行输入/输出值。

  我们可以通过调用有名称的存储过程。

三:格式

  create procedure 名称{(参数1 类型,参数2 类型......)} --参数后不能加括号也不能规定长度

  is | as

    --变量声明区间

  begin

    业务逻辑区域

  end

  /

四:存储过程调用

  -->直接调用  call 存储过程名(有参传参)

   -->plsql调用  

      begin

        存储过程名(有参传参)

      end;

   -->java语言调用

五:练习

  -->使用存储过程输出'hello world'

  create or replace procedure display

  is

    begin

      dbms_output.put_line('hello world');

    end;

    /

  call display();

  -->定义一个存储过程输出参数的内容

  create or replace procedure output(text varchar2)

  is

    begin

      dbms_output.put_line(text);

    end;

    /

  call output('hello plsql');

  -->使用存储过程获取某一员工编号的对应的姓名和职务

  create or replace procedure output(num number)

  as

    a emp.ename%type;

    j emp.job%type;  --自动获取emp表job的类型给j变量的类型

    begin

      select ename,job into a,j from emp where empno=num;

      dbms_output.put_line(a||'  '||j);

    end;

    /

  call output(1101);

六:存储过程可以使用参数返回数据

  特点:此时的参数需要一些属性,in | out | in out

    1)in :表示参数只能被传入数据,不能返回数据,默认状态下就是in属性

    2)out:表示参数只能输出数据,即返回数据。

    3)in out:表示此参数可以输出,也可以输入。

  格式:参数名(in | out | in out) 类型

  练习:传入两个参数计算这两个数的和与差。

    create or replace procedure dis(a in out number,b in out number)

    is

      s number;

      s1 number;

      begin

        s:=a+b;

        s1:=a-b;

        a:=s;

        b:=s1;

      end;

      /

    declare

      a number;

      b number;

      begin

        a:=2;

        b:=3;

        dis(a,b);

        dbms_output.put_line(a||'  '||b);

      end;

      /  

  

猜你喜欢

转载自www.cnblogs.com/lyr999736/p/9051254.html