包:
create or replace package sp_pexam_clear as --定义结构体 /*type re_stu is record( rname student.name%type, rage student.age%type ); --定义游标 type c_stu is ref cursor; */ --定义函数 function numAdd(num1 number,num2 number)return number; --定义过程 --procedure GetStuList(cid in varchar2); end;
包实现:
create or replace package body sp_pexam_clear as --游标和结构体,包规范中已声明,包体中不用再声明,直接使用。 --实现方法 function numAdd(num1 number, num2 number) return number as num number; begin num := num1 + num2; return num; end; --实现过程 /* procedure GetStuList(cid varchar2) as r_stu re_stu; --直接使用包规范中的结构 begin open c_st for select name, age from student where classid = cid; -- 如果已经在过程中遍历了游标,在使用这个过程的块中,将没有值。 -- loop -- fetch c_st into r_stu; -- exit when c_st%notfounad; -- dbms_output.put_line('姓名='||r_stu.rname); -- end loop; end; */ end;
测试下:
-- Created on 2016/7/12 by ACER declare -- Local variables here i integer; num number ; begin -- Test statements here --使用包中的方法 select sp_pexam_clear.numAdd(5, 6) into num from dual; dbms_output.put_line('Num=' || num); end;