天天看点

Oracle存储过程基本语法

转自:http://blog.csdn.net/pg_roger/article/details/8877782

  1  create or replace procedure 存储过程名

  2  is

  3  begin

  4  null;

  5  end;

行1:

  create or replace procedure 是一个sql语句通知oracle数据库去创建一个叫做skeleton存储过程, 如果存在就覆盖它;

行2:

  is关键词表明后面将跟随一个pl/sql体。

行3:

  begin关键词表明pl/sql体的开始。

行4:

  null pl/sql语句表明什么事都不做,这句不能删去,因为pl/sql体中至少需要有一句;

行5:

  end关键词表明pl/sql体的结束

 create or replace procedure 存储过程名(param1 in type,param2 out type) 

as 

变量1 类型(值范围); --vs_msg   varchar2(4000); 

变量2 类型(值范围);

begin

select count(*) into 变量1 from 表a where列名=param1;

    if (判断条件) then

       select 列名 into 变量2 from 表a where列名=param1;

       dbms_output。put_line(‘打印信息’);

    elsif (判断条件) then

    else

       raise 异常名(no_data_found);

    end if;

exception

    when others then

       rollback;

end;

注意事项:

1, 存储过程参数不带取值范围,in表示传入,out表示输出

类型可以使用任意oracle中的合法类型。

2,  变量带取值范围,后面接分号

3,  在判断语句前最好先用count(*)函数判断是否存在该条操作记录

4,  用select 。。。into。。。给变量赋值

5,  在代码中抛异常用 raise+异常名

create or replace procedure存储过程名

(

--定义参数

 is_ym  in char(6) ,

the_count out number,

--定义变量 

vs_msg   varchar2(4000);   --错误信息变量

vs_ym_beg  char(6);      --起始月份

vs_ym_end  char(6);      --终止月份

vs_ym_sn_beg char(6);     --同期起始月份

vs_ym_sn_end char(6);     --同期终止月份

--定义游标(简单的说就是一个可以遍历的结果集) 

cursor cur_1 is 

  select 。。。 

  from 。。。 

    where 。。。

   group by 。。。; 

begin 

--用输入参数给变量赋初值,用到了oralce的substr to_char add_months 

to_date 等很常用的函数。 

vs_ym_beg := substr(is_ym,1,6); 

vs_ym_end := substr(is_ym,7,6); 

vs_ym_sn_beg := to_char(add_months(to_date(vs_ym_beg,'yyyymm'), -12),'yyyymm'); 

vs_ym_sn_end := to_char(add_months(to_date(vs_ym_end,'yyyymm'), -12),'yyyymm'); 

--先删除表中特定条件的数据。 

delete from 表名 where ym = is_ym; 

  --然后用内置的dbms_output对象的put_line方法打印出影响的记录行数,其中用到一个系统变量sql%rowcount 

dbms_output.put_line('del上月记录='||sql%rowcount||'条'); 

insert into表名(area_code,ym,cmcode,rmb_amt,usd_amt) 

select area_code,is_ym,cmcode,sum(rmb_amt)/10000,sum(usd_amt)/10000 

from bgd_area_cm_m_base_t 

  where ym >= vs_ym_beg 

  and ym <= vs_ym_end 

group by area_code,cmcode; 

dbms_output.put_line('ins当月记录='||sql%rowcount||'条'); 

--遍历游标处理后更新到表。遍历游标有几种方法,用for语句是其中比较直观的一种。

for rec in cur_1 loop 

  update 表名

  set rmb_amt_sn = rec.rmb_amt_sn,usd_amt_sn = rec.usd_amt_sn 

   where area_code = rec.area_code 

   and cmcode = rec.cmcode 

   and ym = is_ym; 

end loop; 

commit; 

--错误处理部分。others表示除了声明外的任意错误。sqlerrm是系统内置变量保存了当前错误的详细信息。 

exception 

   when others then 

      vs_msg := 'error in xxxxxxxxxxx_p('||is_ym||'):'||substr(sqlerrm,1,500);

   rollback; 

   --把当前错误记录进日志表。 

   insert into log_info(proc_name,error_info,op_date) 

   values('xxxxxxxxxxx_p',vs_msg,sysdate); 

   commit; 

   return; 

oracle存储过程语法

1 、判断语句: 

if 比较式 then begin end; end if; 

create or replace procedure test(x in number) is 

        if x >0 then 

         begin 

        x := 0 - x; 

        end; 

    end if; 

    if x = 0 then 

       begin 

        x: = 1; 

    end; 

end test; 

2 、for 循环 

for ... in ... loop 

-- 执行语句 

(1) 循环遍历游标 

create or replace procedure test() as 

cursor cursor is select name from student; name varchar(20); 

for name in cursor loop 

 dbms_output.putline(name);  

end; 

(2) 循环遍历数组 

 create or replace procedure test(vararray in mypackage.testarray) as 

--( 输入参数vararray 是自定义的数组类型,定义方式见标题6) 

i number; 

i := 1;  -- 存储过程数组是起始位置是从1 开始的,与java 、c 、c++ 等语言不同。因为在oracle 中本是没有数组的概念的,数组其实就是一张 

-- 表(table), 每个数组元素就是表中的一个记录,所以遍历数组时就相当于从表中的第一条记录开始遍历 

for i in 1..vararray.count loop      

dbms_output.putline('the no.'|| i || 'record in vararray is:'||vararray(i));    

 end loop; 

3 、while 循环 

while 条件语句 loop 

e.g 

create or replace procedure test(i in number) as 

while i < 10 loop 

begin     

 i:= i + 1; 

 end test; 

4 、数组 

首先明确一个概念:oracle 中本是没有数组的概念的,数组其实就是一张表(table), 每个数组元素就是表中的一个记录。 

使用数组时,用户可以使用oracle 已经定义好的数组类型,或可根据自己的需要定义数组类型。 

(1) 使用oracle 自带的数组类型 

x array; -- 使用时需要需要进行初始化 

e.g: 

create or replace procedure test(y out array) is 

 x array;   

 begin 

x := new array(); 

y := x; 

(2) 自定义的数组类型 ( 自定义数据类型时,建议通过创建package 的方式实现,以便于管理) 

create or replace package mypackage is 

   public type declarations   type info is record(     name varchar(20),     y number); 

  type testarray is table of info index by binary_integer;   

-- 此处声明了一个testarray 的类型数据,其实其为一张存储info 数据类型的table 而已,及testarray 就是一张表,有两个字段,一个是name ,一个是y 。需要注意的是此处使用了index by binary_integer 编制该table 的索引项,也可以不写,直接写成:type testarray is 

table of info ,如果不写的话使用数组时就需要进行初始化:vararray mypackage.testarray; vararray := new mypackage.testarray(); 

end testarray; 

5. 游标的使用 oracle 中cursor 是非常有用的,用于遍历临时表中的查询结果。其相关方法和属性也很多,现仅就常用的用法做一二介绍: 

(1)cursor 型游标( 不能用于参数传递) 

create or replace procedure test() is   

cusor_1 cursor is select std_name from student where  ...;  --cursor 的使用方式1   cursor_2 cursor; 

select class_name into cursor_2 from class where ...;  --cursor 的使用方式2 

可使用for x in cursor loop .... end loop; 来实现对cursor 的遍历 

(2)sys_refcursor 型游标,该游标是oracle 以预先定义的游标,可作出参数进行传递 

create or replace procedure test(rscursor out sys_refcursor) is 

cursor sys_refcursor; 

name varhcar(20); 

open cursor for select name from student where ... --sys_refcursor 只能通过open 方法来打开和赋值 

loop 

 fetch cursor into name   --sys_refcursor 只能通过fetch into 来打开和遍历 exit when cursor%notfound;              --sys_refcursor 中可使用三个状态属性:                                         ---%notfound( 未找到记录信息) %found( 找到记录信息)                                         ---%rowcount( 然后当前游标所指向的行位置) 

 dbms_output.putline(name); 

rscursor := cursor; 

实例

下面写一个简单的例子来对以上所说的存储过程的用法做一个应用: 

现假设存在两张表,一张是学生成绩表(studnet) ,字段为:stdid,math,article,language,music,sport,total,average,step               

一张是学生课外成绩表(out_school), 字段为:stdid,parctice,comment 

通过存储过程自动计算出每位学生的总成绩和平均成绩,同时,如果学生在课外课程中获得的评价为a ,就在总成绩上加20 分。 

create or replace procedure autocomputer(step in number) is 

rscursor sys_refcursor; 

commentarray mypackage.myarray; 

math number; 

article number; 

language number; 

music number; 

sport number; 

total number; 

average number; 

stdid varchar(30); 

record mypackage.stdinfo; 

i := 1; 

get_comment(commentarray); -- 调用名为get_comment() 的存储过程获取学生课外评分信息 

open rscursor for select stdid,math,article,language,music,sport from student t where t.step = step; 

fetch rscursor into stdid,math,article,language,music,sport; exit when rscursor%notfound; 

total := math + article + language + music + sport; 

for i in 1..commentarray.count loop  

 record := commentarray(i);     

if stdid = record.stdid then   

 begin      

 if record.comment = 'a' then      

  begin          

 total := total + 20;    

   go to next; -- 使用go to 跳出for 循环        

  end;     

end if;   

end;   

end if; 

<<continue>>  average := total / 5; 

 update student t set t.total=total and t.average = average where t.stdid = stdid; 

end autocomputer; 

-- 取得学生评论信息的存储过程 

create or replace procedure get_comment(commentarray out mypackage.myarray) is 

rs sys_refcursor ; 

comment varchar(1); 

open rs for select stdid,comment from out_school 

 fetch rs into stdid,comment; exit when rs%notfound; 

record.stdid := stdid; 

 record.comment := comment; 

recommentarray(i) := record; 

i:=i + 1; 

end get_comment; 

-- 定义数组类型myarray 

create or replace package mypackage is begin 

type stdinfo is record(stdid varchar(30),comment varchar(1)); 

type myarray is table of stdinfo index by binary_integer; 

end mypackage;

byebye

上一篇: java反射详解
下一篇: PXE技术