PLSQL預存程序(基礎篇)-轉

來源:互聯網
上載者:User

標籤:

我不是專門的開發人員,但預存程序又是很重要的知識,為了能夠很好的記憶,現把這些基礎知識總結一下。預存程序可以實現代碼的充分共用,提高系統效能。

基礎篇       知識回顧

如果經常使用特定操作,哪麼可以考慮基於這些操作使用過程,簡化開發和維護,可以充分實現代碼共用,提高系統效能。

過程的分類:

(一)有過程參數

  (二)沒有過程參數的             1.輸入參數(預設是輸入參數)             2.輸出參數             3.輸入輸出參數基本文法規則:Create or replace procedure procedure_name(argument1 [mode1] datatype1 , argument2 [mode2] datatype2 ……)Is [as]PL/SQL Block;注意:當定義參數時,只能指定資料類型,不能指定長度. 範例一:無參數的過程create or replace procedure a_timeisbegindbms_session.set_nls(‘nls_date_format‘,‘‘‘yyyy-mm-dd‘‘‘);dbms_output.put_line(sysdate);end;/ 執行過程:(調用無參數過程可以直接引用過程名)SQL> exec a_time;2008-03-04                                                                     PL/SQL 過程已成功完成 範例二:具有輸入參數的過程:SQL> create or replace procedure b_insert (i emp.id%type,n emp.name%type)2 is3 begin4 insert into emp values(i,n);5 commit;6 end;SQL> 過程已建立。 執行這個過程:SQL> exec b_insert(‘14‘,‘peter_lin‘);PL/SQL 過程已成功完成。SQL> select * from emp;ID          NAME                                                                ---------- ----------                                                          14         peter_lin                                                           1          DICK_t                                                               可以看到,資料自動插入到了表格中。 範例三:帶有輸出參數的過程:SQL> create or replace procedure c_update(old varchar2,new emp.id%type,nam out emp.name%type)2 is3 begin4 select name into nam from emp where id=old;5 update emp set id=new where id=old;6 commit;7 end;SQL> 過程已建立。 執行這個過程:帶有輸出參數的過程,需要使用變數接收這個輸出值。SQL> declare2 nn emp.name%type;3 begin4 c_update(&old,&new,nn);5 dbms_output.put_line(‘被修改id的員工姓名:‘||nn);6 end;7 /輸入 old 的值: 14輸入 new 的值: 12原值    4: c_update(&old,&new,nn);新值    4: c_update(14,12,nn);被修改id的員工姓名:peter_lin                                                  PL/SQL 過程已成功完成。SQL> select * from emp;ID         NAME                                                                 ---------- ----------                                                          12         peter_lin                                                           1          DICK_t                                                               已選擇2行。 範例四:帶有輸入輸出參數的過程SQL>create or replace procedure in_out2 (n1 in out number,n2 in out number) is3 v1 number;4 v2 number;5 begin6 v1:=trunc(n1/n2);7 v2:=mod(n1,n2);8 n1:=v1;9 n2:=v2;10 end;SQL> 過程已建立。 執行這個過程:SQL>declare2 a1 number:=&n1;3 a2 number:=&n2;4 begin5 in_out(a1,a2);6 dbms_output.put_line(‘除法的商‘||a1||‘,除法的餘數:‘||a2);7 end;SQL> 輸入 n1 的值: 100原值    2: a1 number:=&n1;新值    2: a1 number:=100;輸入 n2 的值: 3原值    3: a2 number:=&n2;新值    3: a2 number:=3;

除法的商33,除法的餘數:1  

PLSQL預存程序(基礎篇)-轉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.