Oracle 存儲過程入門(一)

一,基本入門介紹sql

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

看建立存儲過程的基本定義。注意,帶有[]的都是可選的,無關緊要的。只是語法能經過,固然根據本身須要處理。oracle

仍是從簡單例子開始學習,ide

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的語句)的。oop

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

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中執行以下存儲過程。注意看是怎麼調用的:spa

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

 

執行以後,查看數據,3d

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的存儲過程有個簡單的認識。

相關文章
相關標籤/搜索