亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Oracle 中包的應用

系統 1835 0

? ? ? ?包由兩個分離的部分組成:包頭(PACKAGE)和包體(PACKAGEBODY)。包頭是包的說明部分,是對外的操作接口,對應用是可見的;包體是包的代碼和實現部分,對應用來說是不可見的黑盒。
? ? ? ?出現在包頭中的稱為公有元素,出現在包體中的稱為私有元素,出現在包體的過程(或函數)中的稱為局部變量。

創建包頭的簡要語句如下:

      
        CREATE [OR REPLACE] PACKAGE 包名

{IS
      
      |
      
        AS}

公有變量定義

公有類型定義

公有游標定義

公有異常定義

函數說明

過程說明

END;
      
    

創建包體的簡要語法如下:

      
        CREATE [OR REPLACE] PACKAGE BODY 包名

{IS
      
      |
      
        AS}

私有變量定義

私有類型定義

私有游標定義

私有異常定義

函數定義

過程定義

END;
      
    

其它操作:

      
        刪除包頭:

DROP PACKAGE 包頭名

刪除包體:

DROP PACKAGE BODY 包體名

重新編譯包頭:

ALTER PACKAGE 包名 COMPILE PACKAGE

重新編譯包體:

ALTER PACKAGE 包名 COMPILE PACKAGE BODY
      
    

案例:對學生表infos提供一個增刪改查的包,infos表內容如下圖所示:

包中的內容結構如下:

程序結構 類型 參數 說明
v_infos_count 公有變量 ? 學生總總數量,number類型
p_init 公有過程

p_max number

p_min number

最大值,最小值
p_list_infos 公有過程 ? 顯示學生列表數據
p_add_infos 公有過程

p_stuid infos.stuid%type,
p_stuname infos.stuname%type,
p_gender infos.gender%type,
p_age infos.age%type,
p_seat infos.seat%type,
p_enrolldate infos.enrolldate%type,
p_stuaddress infos.stuaddress%type,
p_classno infos.classno%type

增加一條學生記錄
p_delete_infos 公有過程 p_stuid infos.stuid%type 根據stuid刪除一條學生記錄
p_edit_infos_name 公有過程

p_stuid infos.stuid%type
p_stuname infos.stuname%type

根據stuid修改學生的姓名
v_msg 私有變量 ? show message
v_max_age 私有變量 ? max age ,number
v_min_age 私有變量 ? min age ,number
f_exist_infos 私有函數

p_stuid infos.stuid%type

判斷學生是否存在,

return boolean

p_show_msg 私有過程 ? show msg

包SQL:


        1
      
      
        )創建包頭


      
      
        create
      
      
        or
      
      
        replace
      
      
         package pck_infos


      
      
        as
      
      
        --
      
      
        總數量
      
      

  v_infos_count 
      
        number
      
      
        ;

  
      
      
        --
      
      
        初始化操作
      
      
        procedure
      
       p_init(p_max 
      
        number
      
      , p_min 
      
        number
      
      
        );

  
      
      
        --
      
      
        顯示學生列表數據
      
      
        procedure
      
      
         p_list_infos;

  
      
      
        --
      
      
        增加一條學生記錄
      
      
        procedure
      
      
         p_add_infos(

    p_stuid       infos.stuid
      
      
        %
      
      
        type,

    p_stuname     infos.stuname
      
      
        %
      
      
        type,

    p_gender      infos.gender
      
      
        %
      
      
        type,

    p_age         infos.age
      
      
        %
      
      
        type,

    p_seat        infos.seat
      
      
        %
      
      
        type,

    p_enrolldate  infos.enrolldate
      
      
        %
      
      
        type,

    p_stuaddress  infos.stuaddress
      
      
        %
      
      
        type,

    p_classno     infos.classno
      
      
        %
      
      
        type);

  
      
      
        --
      
      
        刪除一條學生記錄
      
      
        procedure
      
       p_delete_infos(p_stuid infos.stuid
      
        %
      
      
        type);

  
      
      
        --
      
      
        根據stuid修改學生的姓名
      
      
        procedure
      
      
         p_edit_infos_name(

    p_stuid   infos.stuid
      
      
        %
      
      
        type,

    p_stuname infos.stuname
      
      
        %
      
      
        type);


      
      
        end
      
      
        ;

(
      
      
        2
      
      
        )創建包體


      
      
        create
      
      
        or
      
      
        replace
      
      
         package body pck_infos


      
      
        as
      
      
        

  v_msg     
      
      
        varchar2
      
      (
      
        100
      
      );  
      
        --
      
      
        show message
      
      

  v_max_age 
      
        number
      
      ;         
      
        --
      
      
        max age
      
      

  v_min_age 
      
        number
      
      ;         
      
        --
      
      
        min age
      
      
        --
      
      
        判斷學生是否存在
      
      
        function
      
       f_exist_infos(p_stuid infos.stuid
      
        %
      
      
        type)

  
      
      
        return
      
      
         boolean;

  

  
      
      
        --
      
      
        show msg
      
      
        procedure
      
      
         p_show_msg;

  

  
      
      
        --
      
      
        初始化操作
      
      
        procedure
      
       p_init(p_max 
      
        number
      
      , p_min 
      
        number
      
      
        )

  
      
      
        as
      
      
        begin
      
      
        select
      
      
        count
      
      (stuid) 
      
        into
      
       v_infos_count 
      
        from
      
      
         infos;

    v_max_age:
      
      
        =
      
      
        p_max;

    v_min_age:
      
      
        =
      
      
        p_min;

    v_msg:
      
      
        =
      
      
        '
      
      
        init finished!
      
      
        '
      
      
        ;

    p_show_msg;

  
      
      
        end
      
      
         p_init;

  

  
      
      
        --
      
      
        顯示信息
      
      
        procedure
      
      
         p_show_msg

  
      
      
        as
      
      
        begin
      
      
        

    dbms_output.put_line(v_msg);

  
      
      
        end
      
      
         p_show_msg;

   
      
      
        --
      
      
        判斷學生是否存在
      
      
        function
      
       f_exist_infos(p_stuid infos.stuid
      
        %
      
      
        type)

  
      
      
        return
      
      
         boolean

  
      
      
        as
      
      
        

    v_num 
      
      
        number
      
      
        ;

  
      
      
        begin
      
      
        select
      
      
        count
      
      (stuid) 
      
        into
      
       v_num 
      
        from
      
       infos 
      
        where
      
       stuid
      
        =
      
      
        p_stuid;

    
      
      
        if
      
       v_num
      
        =
      
      
        1
      
      
        then
      
      
        return
      
      
         true;

    
      
      
        else
      
      
        return
      
      
         false;

    
      
      
        end
      
      
        if
      
      
        ;

  
      
      
        end
      
      
         f_exist_infos;

  

  
      
      
        --
      
      
        顯示學生列表數據
      
      
        procedure
      
      
         p_list_infos

  
      
      
        as
      
      
        

    v_infos_record infos
      
      
        %
      
      
        rowtype;

    
      
      
        cursor
      
       cur_infos 
      
        is
      
      
        select
      
      
        *
      
      
        from
      
      
         infos;

  
      
      
        begin
      
      
        open
      
      
         cur_infos;

    loop

      
      
      
        fetch
      
       cur_infos 
      
        into
      
      
         v_infos_record;

      
      
      
        exit
      
      
        when
      
       cur_infos
      
        %
      
      
        notfound;

      dbms_output.put_line(
      
      
        '
      
      
        stuid:
      
      
        '
      
      
        ||
      
      
        v_infos_record.stuid);

    
      
      
        end
      
      
         loop;

    
      
      
        close
      
      
         cur_infos;

  
      
      
        end
      
      
         p_list_infos;

  

  
      
      
        --
      
      
        增加一條學生記錄
      
      
        procedure
      
      
         p_add_infos(

    p_stuid       infos.stuid
      
      
        %
      
      
        type,

    p_stuname     infos.stuname
      
      
        %
      
      
        type,

    p_gender      infos.gender
      
      
        %
      
      
        type,

    p_age         infos.age
      
      
        %
      
      
        type,

    p_seat        infos.seat
      
      
        %
      
      
        type,

    p_enrolldate  infos.enrolldate
      
      
        %
      
      
        type,

    p_stuaddress  infos.stuaddress
      
      
        %
      
      
        type,

    p_classno     infos.classno
      
      
        %
      
      
        type)

  
      
      
        as
      
      
        begin
      
      
        if
      
      
        not
      
       f_exist_infos(p_stuid) 
      
        then
      
      
        insert
      
      
        into
      
      
         infos(stuid,stuname,gender,age,seat,enrolldate,stuaddress,classno)

        
      
      
        values
      
      
        (p_stuid,p_stuname,p_gender,p_age,p_seat,p_enrolldate,p_stuaddress,p_classno);

      
      
      
        commit
      
      
        ;

      v_infos_count:
      
      
        =
      
      v_infos_count
      
        +
      
      
        1
      
      
        ;

    
      
      
        else
      
      
        

      v_msg:
      
      
        =
      
      
        '
      
      
        already exist!
      
      
        '
      
      
        ;

    
      
      
        end
      
      
        if
      
      
        ;

  
      
      
        end
      
      
         p_add_infos;

  

  
      
      
        --
      
      
        刪除一條學生記錄
      
      
        procedure
      
       p_delete_infos(p_stuid infos.stuid
      
        %
      
      
        type)

  
      
      
        as
      
      
        begin
      
      
        if
      
       f_exist_infos(p_stuid) 
      
        then
      
      
        delete
      
      
        from
      
       infos 
      
        where
      
       stuid
      
        =
      
      
        p_stuid;

      
      
      
        commit
      
      
        ;

      v_infos_count:
      
      
        =
      
      v_infos_count
      
        -
      
      
        1
      
      
        ;

    
      
      
        else
      
      
        

      v_msg:
      
      
        =
      
      
        '
      
      
        not exist infos!
      
      
        '
      
      
        ;

    
      
      
        end
      
      
        if
      
      
        ;

  
      
      
        end
      
      
         p_delete_infos;

  

   
      
      
        --
      
      
        根據stuid修改學生的姓名
      
      
        procedure
      
      
         p_edit_infos_name(

    p_stuid   infos.stuid
      
      
        %
      
      
        type,

    p_stuname infos.stuname
      
      
        %
      
      
        type)

  
      
      
        as
      
      
        begin
      
      
        if
      
       f_exist_infos(p_stuid) 
      
        then
      
      
        update
      
       infos 
      
        set
      
       stuname
      
        =
      
      p_stuname 
      
        where
      
       stuid
      
        =
      
      
        p_stuid;

      
      
      
        commit
      
      
        ;

    
      
      
        else
      
      
        

      v_msg:
      
      
        =
      
      
        '
      
      
        not exists infos
      
      
        '
      
      
        ;

    
      
      
        end
      
      
        if
      
      
        ;

  
      
      
        end
      
      
         p_edit_infos_name;

    


      
      
        end
      
      ;
    

?

Oracle 中包的應用


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 一级毛片一级毛片免费毛片 | 99热热久久这里只有精品166 | 中文字幕一区二区三区亚洲精品 | 黄页成人免费网站 | 综合图区亚洲白拍在线 | 天天干天天做天天操 | 国产成人91一区二区三区 | 欧美操片在线观看 | 亚洲日韩中文字幕在线播放 | 狠狠添| 色综合久久久久久久久五月 | www亚洲精品| 久久一色本道亚洲 | 精品一区二区视频在线观看 | 日韩视频一区二区 | 国产成人亚洲精品久久 | 色五婷婷 | 成人亚洲国产综合精品91 | 国产福利在线看 | 男女羞羞免费视频 | 最近中文字幕在线 | 中文 | 久久香蕉国产线看观看亚洲片 | 国产一区二区三区免费播放 | 91精选视频 | 在线观看免费亚洲 | 成人在线一区二区三区 | 国内精品一区二区2021在线 | 欧美一区中文字幕 | 久久久国产精品免费视频 | 亚洲 欧美 日韩 综合 | 欧美人与动人物a级网站 | 久久99久久99精品免观看动漫 | 羞羞视频网站在线观看 | 日韩欧美视频一区二区三区 | 久久爱www.| 久久精品7 | 久久中文字幕网 | 国产午夜爽爽窝窝在线观看 | 亚洲欧美日韩国产综合 | 国产成人精品区在线观看 | 久久久精品久久久久久久久久久 |