读《Oracle PLSQL 程序设计第五版》创建包规范和包体

create or replace package favorites_pkg
authid current_user 
--authid字句,用来确定在引用包中的数据对象是应该根据包的属主(authid definer)来解析
--还是根据包的调用者(authid current_user)来解析
is /*or as*/
--两个常量
c_chocolate constant pls_integer := 16;
c_strawberry constant pls_integer := 29;

--用type声明一个嵌套表类型
type codes_nt is table of integer;

--根据之前那个类型声明的一个嵌套表变量
my_favorites codes_nt;

--  一个返回信息的ref cursor 游标类型
type fav_info_rct is ref cursor return favorites%rowtype;

--  一个过程,接收信息列表,然后显示列表中的信息
procedure show_favorites(list_in in codes_nt);

--  一个函数,返回信息中的内容
function most_popular return fav_info_rct;

end favorites_pkg;

创建完包规范后,创建包体。

create or replace package body favorites_pkg
--不能在包体中使用authid字句,其必须出现在包规范中。
is
--  一个私有变量
g_most_popular pls_integer := c_strawberry;

--函数的实现部分
function most_popular return fav_info_rct
  is
  retval fav_info_rct;
  null_cv fav_info_rct;
  begin
    open retval for
         select * from favorites where id = g_most_popular;
    return retval;
  exception
    when no_data_found then return null_cv;
end most_popular;

--过程的实现部分
procedure show_favorites (list_in in codes_nt) is
  begin
    for indx in list_in.first .. list_in.last
      loop
        dbms_output.put_line(list_in (indx));
      end loop;
end show_favorites;

--初始化单元
begin
g_most_popular = c_chocolate ;

end favorites_pkg;

  

  

转载于:https://www.cnblogs.com/liuzhe/p/5198016.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值