Oracle 存儲過程入門(一)


一,基本入門介紹

公司系統需要用到oracle,可是還沒在項目用過oracle,好吧,從基本學起。有問題的地方,歡迎指導啊。

看創建存儲過程的基本定義。注意,帶有[]的都是可選的,可有可無的。只是語法能通過,當然根據自己需要處理。

還是從簡單例子開始學習,

CREATE [OR REPLACE] PROCEDURE procedure_name
    [ (parameter [,parameter]) ]

IS
    [declaration_section]

BEGIN
    executable_section

[EXCEPTION
    exception_section]

END [procedure_name];
View Code

 

先初始化數據。我用的是Toad工具,下面的代碼是在SQL Editor 中執行的。

create table students
( 
  ID int,
  userName varchar(100),
  userPass varchar(100),
  userAge  int
)

insert into students values(1,'jack','jjjaa',23);
insert into students values(2,'rose','jjjaa',21);
insert into students values(3,'lucy','jjjaa',22);
insert into students values(4,'Tony','jjjaa',24);
commit;
View Code

 

當然,新建存儲過程是需要在Procedure Editor中編寫,但是執行存儲過程又需要在SQL Editor中去執行,Procedure中是不可以執行(like exec的語句)的。

這里我們新建一個存儲過程,對於某個用戶添加年齡,哈哈,當然這個是沒什么意義的,學習,從簡單入手。在實際開發中,語法,原理是一樣的。

create or replace procedure  SP_Update_Age
(
 uName in varchar,--note,here don't have length ,sql have lenth ,not in oracle.
 Age in int
)
as
begin
    update students set UserAge = UserAge + Age where userName = uName;
    commit;
end SP_Update_Age;  
View Code

 

在執行存儲過程之前,我們先查看原來的數據。

select * from students


/*********************

ID    USERNAME    USERPASS    USERAGE

1    jack            jjjaa        23
2    rose            jjjaa        21
3    lucy            jjjaa        22
4    Tony            jjjaa        24

**********************/
View Code

 

然后我們在SQL Editor中執行如下存儲過程。注意看是怎么調用的:

exec SP_UPDATE_AGE('jack',1);
View Code

 

執行之后,查看數據,

select * from students;

/********************

ID    USERNAME    USERPASS    USERAGE

1    jack    jjjaa    24  --noted,have changed 
2    rose    jjjaa    21
3    lucy    jjjaa    22
4    Tony    jjjaa    24

*********************/
View Code

 

二,基本語法介紹

可以看出,基本的功能實現,調用完成。

下面,來看看基本語法:

1,變量賦值

 變量名 := 值;

2,判斷語句。

if

比較式

then

begin

end;

end

if

結合起來寫個簡單例子:

create or replace procedure Test(x in out number)
is
begin
     if x<0 then
         begin
             x:= 0 - x;
        end;
     elsif x > 0 then     --noted here elsif 
         begin
             x:= x ;
        end;
     else
        x:=    0;
     end if;
end Test;
View Code

 

 

Test:

set serveroutput on;  --沒這句話,看不到dmbs_output信息。
declare
       num number;
begin
    num:= -1;
    test(num);
    dbms_output.put_line( 'num = ' || num );
end;
/******************************
num = 1
PL/SQL procedure successfully completed.
*******************************/
View Code

 

 

3,For循環,

For  in ..loop;

set serveroutput on;
DECLARE
   x NUMBER := 100;
BEGIN
   FOR i IN 1..10 LOOP  --noted here 
      IF MOD(i,2) = 0 THEN     -- i is even
          dbms_output.put_line( 'i: '||i||' is even ' );
      ELSE
          dbms_output.put_line('i: '|| i||' is odd' );
      END IF;
      x := x + 100;
      dbms_output.put_line('x value: '|| x);
   END LOOP;
   COMMIT;
END;

/*************************
i: 1 is odd
x value: 200
i: 2 is even 
x value: 300
i: 3 is odd
x value: 400
i: 4 is even 
x value: 500
i: 5 is odd
x value: 600
i: 6 is even 
x value: 700
i: 7 is odd
x value: 800
i: 8 is even 
x value: 900
i: 9 is odd
x value: 1000
i: 10 is even 
x value: 1100
PL/SQL procedure successfully completed.


*************************/
View Code

 

 

后面再說遍歷什么游標啊,數組啊。先從簡單的 開始。

4,While 循環。

create or replace Procedure Test2(i in out number)
as
begin
     while i < 10 loop
            begin
                   i:= i+1;
           end;
          end loop;
end Test2;
View Code

 

來測試下。

set serveroutput on;
declare
       num number;
begin
    num:= 1;
    test2(num);
    dbms_output.put_line( 'num = ' || num );
end;


/*********************

num = 10
PL/SQL procedure successfully completed.

***********************/
View Code

 

 

第一篇就先寫到這里,對Oracle的存儲過程有個簡單的認識。

 


免責聲明!

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



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