1. 程式人生 > >ORACLE 儲存過程學習(一)

ORACLE 儲存過程學習(一)

一,基本入門介紹

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

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

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

按 Ctrl+C 複製程式碼

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

IS
[declaration_section]

BEGIN
executable_section

[EXCEPTION
exception_section]

END [procedure_name];
按 Ctrl+C 複製程式碼

先初始化資料。我用的是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;
複製程式碼

當然,新建儲存過程是需要在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;
複製程式碼

在執行儲存過程之前,我們先檢視原來的資料。

複製程式碼
select * from students

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

ID USERNAME USERPASS USERAGE

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

**********************/
複製程式碼

然後我們在SQL Editor中執行如下儲存過程。注意看是怎麼呼叫的:

exec SP_UPDATE_AGE(‘jack’,1);

執行之後,檢視資料,

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;
複製程式碼

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.
*******************************/
複製程式碼

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.

*************************/
複製程式碼

後面再說遍歷什麼遊標啊,陣列啊。先從簡單的 開始。

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;
複製程式碼

來測試下。

複製程式碼
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.

***********************/
複製程式碼

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

共同學習,共同進步!