欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

oracle存储过程demo分享

程序员文章站 2022-06-21 07:55:38
--1.定义存储过程 --定义 create or replace procedure proc01 as n int;         --注意定义变量后面...

--1.定义存储过程

--定义

create or replace procedure proc01

as n int;         --注意定义变量后面要加分号

begin

  select count(*) into n from emp;

  dbms_output.put_line('员工表中有'||n||'条记录!');

end;

--调用

declare

begin

  proc01();

end;

--2.带参数的存储过程

--定义

create or replace procedure p3(j1 varchar2)

as

cursor c1 is select * from emp where job=j1;

emper emp%rowtype;

begin

  open c1;  

  loop

    fetch c1 into emper;

    exit when c1%notfound;

    dbms_output.put_line(emper.ename);

  end loop;

  close c1;

end;

--调用

declare

begin

  p3('clerk');

end;

--带有输入,输出参数的过程

create or replace procedure p4(a in int,b in int,c out int)

as

begin

  c:=0;           --给c赋初始值

  for i in a..b

    loop

    c:=c+i;       --累加

    end loop;

end;

--调用

declare

    a int:=1;

    b int:=100;

    c int;

begin

  p4(a,b,c);      --调用

  dbms_output.put_line(c);

end;

--求给定数字的阶乘(带输入和输出参数)

create or replace procedure p5(a in int,b out int)

as

begin

  b:=1;

  for i in 1..a   --a的阶乘

    loop

      b:=b*i;

    end loop;

end;

--调用

declare

    a int:=5;

    b int;

begin

  p5(a,b);

  dbms_output.put_line(b);

end;

--求给定数字的阶乘(又是输入又是输出参数)(借助了b这个中间参数)

create or replace procedure p6(a in out int)

as

       b int;

begin

  b:=a;  --把a的值赋值给b

  a:=1;  --将a的值修改为1

  for i in 1..b      --循环

    loop

      a:=a*i;

    end loop;

end;

--调用

declare

    a int:=6;

begin

  p6(a);

  dbms_output.put_line(a);

end;