oracle預存程序–入門與調試

來源:互聯網
上載者:User

oracle預存程序--入門與調試

例子如下:

create or replace procedure pro_one(p_one   in number,                                    p_two   out varchar2,                                    p_three in out varchar2) is  --as  --變數聲明部分  v_str1 varchar2(32) := '初始值方式1';  v_str2 varchar2(32) default '初始值方式2';  v_str3 varchar2(32); --空值  v_num4 number default 0;  --代碼塊部分  /*代碼塊至少要有一句可執行檔代碼,否則報錯,如果什麼也不做,可以寫NULL;*/begin  dbms_output.put_line('傳參:' || 'p_one:' || p_one);  dbms_output.put_line('變數:' || v_str1 || ' , ' || v_str2 || ' , ' ||                       v_str3 || ' , ' || v_num4);  p_three := p_three || 'changed';  select count(*) into v_num4 from lsy_codebook where id = p_one;  if v_num4 <= 0 then    begin      p_two   := '';      p_three := '資料庫無此記錄!';      dbms_output.put_line('資料庫無此記錄!');    end;  elsif v_num4 > 1 then    raise too_many_rows; --拋出異常  else    begin      update lsy_codebook set description = p_three where id = p_one;      commit;      p_two   := p_three;      p_three := p_one || ' update success';    end;  end if;  --異常捕獲exception  when no_data_found then    begin      dbms_output.put_line('not found ,update failed.');      p_three := 'Exception Info:' || p_one || ' not found ,update failed.';    end;  when too_many_rows then    begin      dbms_output.put_line('too_many_rows ,update failed.');      p_three := 'Exception Info:' || p_one ||                 ' too_many_rows ,update failed.';    end;  when others then    begin      rollback;      p_three := 'Exception Info:' || p_one ||                 ' others exception ,update failed.';    end;end pro_one;

從上可以看出,預存程序的定義結構分3部分。一是聲明部分,這裡初次簡曆預存程序是最好使用create  procedure xxx,因為一旦你起的名字在庫中已經存在,你就把存在的覆蓋掉了。這個機率很小,但是錯誤很低級。建立後再改成create or replace procedure xxx。二是變數聲明部分,這個部分可以沒有,如果你不用到變數的話。對比java的類,這個部分像聲明全域變數。三就是代碼塊部分。在begin end預存程序名之間。在預存程序中我們使用begin 和 end 來代替我們習慣的{}。個人感覺{}更好,但是甲骨文的那幫人有自己的想法。

需要注意的有:預存程序方法可以沒有任何參數,例create or replace procedure pro_one is。參數的輸入輸出類型有3種,in,out,in out。簡單說就是in類型的參數在參進來的時候就有值,它只能被讀取,不能被賦值;out類型只能被賦值,不能把值付給別的變數。In out類型包含以上兩種功能。為什麼會這樣設計呢?對比java,預存程序有參數,所以像方法。而預存程序的傳回值是沒有的,不能像java方法中用等號來擷取結果,所以它的傳回值只能賦給參數變數,來儲存結果。在參數列表中參數的類型不能有長度定義,在變數的聲明塊中,變數必須有長度定義。

文法很簡單,這篇主要講怎麼使用pl/sql調試。

建立預存程序可以世界用sql window。最好用program  window。或者在對象面板中找到Procedures右鍵new。建立後用program  window開啟才會有錯誤提示資訊(右鍵edit)。

F8執行後,如果有錯誤或警告,就會提示。

預存程序的調試步驟:

右鍵Add debug infomation。右鍵test。就會開啟測試視窗。

左上方的齒輪或者F9即使開始執行。

在下方會把變數列出來,我們可以在下方輸入變數的值,看代碼中的=>指向下方變數名。

這是plsql提供的一個方便的功能,我們可以不用它,在代碼部分自己生命變數,再傳入。

斷點設定;

在需要的地方,在程式碼的左邊可以點擊就可以添加斷點(紅色的是斷點,綠色的是書籤)。

或者右鍵選擇set breakpoint。

對上面的輸入我們可以看看列印和變數結果。

現在我們不用plsql的變數關聯輸入,我們在代碼中自訂變數,來測試。

 

Declarep_one number :=501;p_two varchar2(32) :='';p_three varchar2(32) :='new value 333';begin  -- Call the procedure  --null;  dbms_output.put_line('調用前:p_one:'||p_one||',p_two:'||p_two||',p_three:'||p_three);  pro_one(p_one,p_two, p_three);  dbms_output.put_line('調用後:p_one:'||p_one||',p_two:'||p_two||',p_three:'||p_three);end;

結果的查看和下面的變數列表就沒關係了,我們從列印看資訊:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.