SNO SNAME SAGE --------------------------------------- ---------- --------------------------------------- 5 lisi -6 1 Tom 31 2 Kite 32 3 Bob 33 4 Mike 34
首先创建如下程序包包头:
1 2 3 4 5 6 7
createorreplacepackage pack1 is aa int := -9; -- 公共变量 procedure insert_student(a1 in student%rowtype); -- 声明一个存储过程 procedure update_student(a2 in student%rowtype); -- 声明一个存储过程 end pack1; /
createorreplacepackagebody pack1 -- 增加一个 body 且名字相对应 is bb int := 5; -- 声明一个私有变量 -- 定义第一个存储过程 procedure insert_student(a1 in student%rowtype) is begin insertinto student(sno, sname, sage) values (a1.sno, a1.sname, a1.sage); commit; -- 验证一下私有变量 dbms_output.put_line(pack1.bb); end insert_student; -- 定义第二个存储过程 procedure update_student(a2 in student%rowtype) is begin update student set sname = a2.sname, sage = a2.sage where sno = a2.sno; commit; end update_student; end pack1; /
-- 创建包头 createorreplacepackage pack_ref is type refcur isrefcursor; -- 此处只声明类型为参照游标的类型变量 procedure mycursor_use; -- 声明存储过程 end pack_ref; /
-- 创建包体 createorreplacepackagebody pack_ref is procedure mycursor_use is mycursor refcur; -- 此处声明类型为 refcur 的变量 stu_rec student%rowtype; begin open mycursor forselect * from student; LOOP fetch mycursor into stu_rec; EXIT WHEN mycursor%NOTFOUND; DBMS_OUTPUT.put_line('学号:' || stu_rec.sno || ',姓名:' || stu_rec.sname); ENDLOOP; close mycursor; end mycursor_use; end pack_ref; /
SQL> SELECT text FROM USER_SOURCE WHERE NAME='PACK_REF'; ---注意大写
TEXT -------------------------------------------------------------------------------- package pack_ref is type refcur is ref cursor; -- 此处只声明类型为参照游标的类型变量 procedure mycursor_use; -- 声明存储过程 end pack_ref; package body pack_ref is procedure mycursor_use is mycursor refcur; -- 此处声明类型为 refcur 的变量 stu_rec student%rowtype; begin open mycursor forselect * from student; LOOP fetch mycursor into stu_rec; EXIT WHEN mycursor%NOTFOUND; DBMS_OUTPUT.put_line('学号:' || stu_rec.sno || ',姓名:' || stu_rec.sname); ENDLOOP; close mycursor; end mycursor_use; end pack_ref;