七、PL/SQL存儲函數和存儲過程及打斷點如何調試


1、存儲過程和函數的概念:

ORACLE 提供可以把PL/SQL 程序存儲在數據庫中,並可以在任何地方來運行它。這樣就叫存儲過程或函數。過程和函數統稱為PL/SQL子程序,他們是被命名的PL/SQL塊,均存儲在數據庫中,並通過輸入、輸出參數或輸入/輸出參數與其調用者交換信息。過程和函數的唯一區別是函數總向調用者返回數據,而過程則不返回數據

常見的單詞:

pragma 編譯指示

instantiable  實例化

overriding 覆蓋重寫

static member 靜態成員

delimited 划定…的界限

identifier 標識符

reverse 反向

2、存儲函數的格式

|存儲函數格式|

create or replace function func_name(dept_id number,salary number)

return varchar2

is

  -- 函數使用過程中,需要聲明的變量,記錄類型,cursor

      

begin 

  --函數的執行體,如果有返回值需要return

    return 'helloworld'|| v_logo;

       --commit;如果此處是進行insert、delete、update操作,可以通過提交進行更改,無需再調用存儲函數后再進行提交

exception 

   --處理函數執行過程中的異常

end;

詳解:

1)因為函數需要返回一個值, 所以RETURN 包含返回結果的數據類型.

2)函數名后面是一個可選的參數列表, 其中包含IN, OUTIN OUT 標記. 參數之間用逗號隔開

IN 參數標記表示傳遞給函數的值在該函數執行中不改變;

OUT 標記表示一個值在函數中進行計算並通過該參數傳遞給調用語句;

IN OUT 標記表示傳遞給函數的值可以變化並傳遞給調用語句. 若省略標記, 則參數隱含為IN

3

A、存儲函數:有返回值,創建完成后,如何調用:

方法一

select function() from dual;

方法二:

set serveroutput on;

var aaa varchar2(10);

call hello_world() into :aaa;

方法三:

begin

    dbms_output.put_line(hello_world());

end;

B、存儲過程:由於沒有返回值,創建完成后,不能使用select語句,只能使用pl/sql塊執行

|實例一:創建一個無參函數| 

create or replace function hello_world

return varchar2

is

begin 

    return 'hello world';

end;

 

如何調用:

方法一

select hello_world() from dual;

方法二

set serveroutput on;

var aaa varchar2(10);

call hello_world() into :aaa;

方法三:

begin

    dbms_output.put_line(hello_world());

end;

方法四:

declare

      v_bianling varchar2(40);

begin

      v_bianling := hello_world;

      dbms_output.put_line(v_bianling);

end;

 

|實例二:創建一個有參函數(傳進去的參數v_classid的值是不變的)| 

求一個班學生的總薪水

create or replace function get_sal(v_classid number)

return number

is 

       v_sumsal number(10):=0;

       cursor sal_cursor is select sal from  student where classid =v_classid;  

begin

       for c in sal_cursor loop

      v_sumsal:=v_sumsal+c.sal;

      end loop;

      return v_sumsal;

 end;

 

|實例三:創建一個有參函數(傳進去的參數v_classid的值是不變的)| 

OUT型參數  對於實例二中的傳進去的參數一般是不變的

In是輸入類型,而out是輸入輸出類型的

如果一個形參用out修飾 那么它就會在函數中被賦值,並且可以當成一個數傳出去(結合例子理解)

create or replace function get_sal1(v_classid number,total_sal out number)

return number

is 

       v_sumsal number(10):=0;

       cursor sal_cursor is select sal from  student where classid =v_classid;  

begin

       total_sal:=0;

       for  c in sal_cursor loop

       v_sumsal:=v_sumsal+c.sal;

       total_sal:=total_sal +1;

       end loop;

       return v_sumsal;

 end;

 

如何調用:

declare

       v_total_sal number(5);

begin

       dbms_output.put_line(get_sal1(1, v_total_sal));

       dbms_output.put_line(v_total_sal);

end;

 

無返回值 但是可以定義一個 out型參數把 值傳出來的例子如下:

create or replace function get_sal2(v_classid number,total_sal out number)

return number

is

       cursor sal_cursor is select sal from  student where classid =v_classid;  

begin

       total_sal:=0;

       for  c in sal_cursor loop

      total_sal:= total_sal+c.sal;

      end loop;

return total_sal;

 end;

如何調用:

declare 

 v_classid   number(5):=1;

       v_total_sal  number(10):=0;

begin 

      dbms_output.put_line(get_sal2(v_classid,v_total_sal));

      dbms_output.put_line(v_total_sal);

end;

3.plsqldep存儲過程如何打斷點進行調試

1)edit pkg_regulatory_tax.;  
在相應的代碼處添加斷點、點擊執行按鈕進行編譯
2)test pkg_regulatory_tax.p_load_auto;輸入傳參信息,點擊上面的執行按鈕進行調試
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM