預存程序的簡單應用

來源:互聯網
上載者:User

預存程序的簡單應用

預存程序:執行一個任務,該任務包括了一系列的PL SQL語句,儲存在資料庫中,成為資料庫一個對象。效率比較高的,但你建立一個預存程序它會進行一個判斷編譯的。

===================================

SQL> CREATE OR REPLACE PROCEDURE xs_proc
  2  IS
  3  BEGIN
  4  NULL;
  5  END;
  6  /
Procedure created.


SQL> EXECUTE xs_proc;
PL/SQL procedure successfully completed.

或者執行

SQL> BEGIN
  2  xs_proc;
  3  END;
  4  /

PL/SQL procedure successfully completed.

================================

SQL> CREATE OR REPLACE PROCEDURE xs_proc
  2  IS
  3  BEGIN
  4  DBMS_OUTPUT.PUT_LINE('hello');
  5  END;
  6  /

SQL> EXECUTE xs_proc;

要設定為ON , 才會把 hello 顯示出來
SQL> SET SERVEROUTPUT ON

SQL> EXECUTE xs_proc;

==================================================

CREATE TABLE xue_sheng( id integer, xing_ming varchar(25), yu_wen number,shu_xue number);

INSERT INTO xue_sheng VALUES(1,'ZhanSan',80,90);

INSERT INTO xue_sheng VALUES(2,'LiSi',85,87);

==================================================

SQL> CREATE OR REPLACE PROCEDURE xs_proc(temp_id IN integer)
  2  IS
  3  name varchar2(25);
  4  BEGIN
  5 select xing_ming into name from xue_sheng where id=temp_id;
  6   DBMS_OUTPUT.PUT_LINE(name);
  7  END;
  8  /

SQL> execute xs_proc(1);
ZhanSan

------------------

當輸入學生的名字, 就會把他的總分(語文+數學)顯示出來。

CREATE OR REPLACE PROCEDURE xs_proc(temp_name IN varchar2 )
IS
num_1 number;
num_2 number;
BEGIN
 select yu_wen,shu_xue into num_1,num_2 from xue_sheng where xing_ming=temp_name;
 DBMS_OUTPUT.PUT_LINE(num_1 + num_2);
END;
/


SQL> EXECUTE xs_proc('ZhanSan');
170


====================================================

SQL> CREATE OR REPLACE PROCEDURE xs_proc(temp_name IN varchar2,temp_num OUT number )
  2  IS
  3  num_1 number;
  4  num_2 number;
  5  BEGIN
  6  select yu_wen,shu_xue into num_1,num_2 from xue_sheng where xing_ming=temp_name;
  7  temp_num := num_1 + num_2;
  8  END;
  9  /

Procedure created.


SQL> DECLARE
  2  tname varchar2(25);
  3  tnum number;
  4  BEGIN
  5  tname:='ZhanSan';
  6  xs_proc( tname,tnum );
  7  DBMS_OUTPUT.PUT_LINE( tnum );
  8  END;
  9  /
170

PL/SQL procedure successfully completed.

=============================================

  1、查看過程狀態

    SELECT object_name,status FROM USER_OBJECTS WHERE object_type='PROCEDURE';

  2、重新編譯過程

    ALTER PROCEDURE xs_proc COMPILE;

  3、查看過程的原始碼

    SELECT * FROM USER_SOURCE WHERE TYPE='PROCEDURE';

  4、刪除預存程序

    DROP PROCEDURE xs_proc;

相關文章

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.