1. 程式人生 > >Oracle儲存過程(1)

Oracle儲存過程(1)

(一)儲存過程的基本語法

  1  CREATE OR REPLACE PROCEDURE 儲存過程名

   IS

   BEGIN

  4  NULL;

   END;

(二)列印第一個Hello word

       1.儲存過程

       create or replace procedure sayhelloword
      as       --------說明部分       begin               dbms_output.put_line("Hello word");       end;       /
        開啟控制檯->切換路徑d: >開啟oracle資料庫:sqlplus scott/
[email protected]
:1521/orcl > 清屏 host cls > 開啟控制檯輸出開關 :set serveroutput  on 

      2.呼叫儲存過程

         1.exec  sayhelloworld();         2.begin                        sayhelloworld();
                       sayhelloworld();             end;              /

      3.呼叫一個帶引數的儲存過程

         1.create  or replace procedure raisesalary(eno in number)             as            --定義一個變數儲存漲前的薪水            psal emp.sal%type;            begin            --得到漲之前的薪水            select  sal into psal  from emp  where empno = eno;            --給員工漲100             update  emp set sal= sal+100 where empno = eno;            --一般不在儲存過程中commit或rollback            --列印           dbms_output.put_line('漲前:'||psal||'   漲後:'||(psal+100));         end;            /                       

  3.儲存函式

          Function 為命名的儲存程式  ,可帶引數  返回計算值           函式和過程的結構類似,但必須有一個return字句 ,用於返回函式值                    建立儲存函式的語法          create or replace  FUNCTION 函式名(引數列表)          return  函式值型別          AS          PLSQL子程式體         
           --儲存函式:查詢員工的年收入             create or replace  function  queryempincome(eno  in number)                    return  number             as             --定義變數儲存員工的薪水和獎金             psal  emp.sal%type             pcommn  emp.comm%type;             begin                       select sal,comm into psal,pcomm from emp where empno=eno;             --直接返回收入             return psal*12+pcomm;             end                                                             

     4.除錯儲存過程 

先用sys賬戶登入 密碼
as sysdba
再對scott進行授權 grant DEBUG CONNECT SESSION ,DEBUG ANY PROCEDURE to scott;