mysql儲存過程詳解
1.
我們常用的運算元據庫語言SQL語句在執行的時候需要要先編譯,然後執行,而儲存過程(Stored Procedure)是一組為了完成特定功能的SQL語句集,經編譯後儲存在資料庫中,使用者通過指定儲存過程的名字並給定引數(如果該儲存過程帶有引數)來呼叫執行它。
一個儲存過程是一個可程式設計的函式,它在資料庫中建立並儲存。它可以有SQL語句和一些特殊的控制結構組成。當希望在不同的應用程式或平臺上執行相同的函式,或者封裝特定功能時,儲存過程是非常有用的。資料庫中的儲存過程可以看做是對程式設計中物件導向方法的模擬。它允許控制資料的訪問方式。
儲存過程通常有以下優點:
(1).儲存過程增強了SQL語言的功能和靈活性。儲存過程可以用流控制語句編寫,有很強的靈活性,可以完成複雜的判斷和較複雜的運算。
(2).儲存過程允許標準元件是程式設計。儲存過程被建立後,可以在程式中被多次呼叫,而不必重新編寫該儲存過程的SQL語句。而且資料庫專業人員可以隨時對儲存過程進行修改,對應用程式原始碼毫無影響。
(3).儲存過程能實現較快的執行速度。如果某一操作包含大量的Transaction-SQL程式碼或分別被多次執行,那麼儲存過程要比批處理的執行速度快很多。因為儲存過程是預編譯的。在首次執行一個儲存過程時查詢,優化器對其進行分析優化,並且給出最終被儲存在系統表中的執行計劃。而批處理的Transaction-SQL語句在每次執行時都要進行編譯和優化,速度相對要慢一些。
(4).儲存過程能過減少網路流量。針對同一個資料庫物件的操作(如查詢、修改),如果這一操作所涉及的Transaction-SQL語句被組織程儲存過程,那麼當在客戶計算機上呼叫該儲存過程時,網路中傳送的只是該呼叫語句,從而大大增加了網路流量並降低了網路負載。
(5).儲存過程可被作為一種安全機制來充分利用。系統管理員通過執行某一儲存過程的許可權進行限制,能夠實現對相應的資料的訪問許可權的限制,避免了非授權使用者對資料的訪問,保證了資料的安全。
2.
儲存過程是資料庫儲存的一個重要的功能,但是MySQL在5.0以前並不支援儲存過程,這使得MySQL在應用上大打折扣。好在MySQL 5.0終於開始已經支援儲存過程,這樣即可以大大提高資料庫的處理速度,同時也可以提高資料庫程式設計的靈活性。
3.
(1). 格式
MySQL儲存過程建立的格式:CREATE
PROCEDURE 過程名 ([過程引數[,...]])
[特性 ...] 過程體
這裡先舉個例子:
<wbr><wbr><wbr>
-
mysql>
DELIMITER // <wbr><wbr><wbr> -
mysql>
<wbr> CREATE<wbr> PROCEDUREproc1(<wbr> OUTs int)<wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr> <wbr> COUNT(*)<wbr> INTOs FROM<wbr><wbr> <wbr> user;<wbr> -
-> <wbr><wbr><wbr><wbr><wbr> <wbr> -
-> // <wbr><wbr><wbr><wbr><wbr><wbr> -
mysql>
DELIMITER ; <wbr><wbr><wbr>
注:<wbr>
(1)這裡需要注意的是DELIMITER //和DELIMITER ;兩句,DELIMITER是分割符的意思,因為MySQL預設以";"為分隔符,如果我們沒有宣告分割符,那麼編譯器會把儲存過程當成SQL語句進行處理,則儲存過程的編譯過程會報錯,所以要事先用DELIMITER關鍵字申明當前段分隔符,這樣MySQL才會將";"當做儲存過程中的程式碼,不會執行這些程式碼,用完了之後要把分隔符還原。
(2)儲存過程根據需要可能會有輸入、輸出、輸入輸出引數,這裡有一個輸出引數s,型別是int型,如果有多個引數用","分割開。
(3)過程體的開始與結束使用BEGIN與END進行標識。
這樣,我們的一個MySQL儲存過程就完成了,是不是很容易呢?看不懂也沒關係,接下來,我們詳細的講解。
(2). 宣告分割符
其實,關於宣告分割符,上面的註解已經寫得很清楚,不需要多說,只是稍微要注意一點的是:如果是用MySQL的Administrator管理工具時,可以直接建立,不再需要宣告。
(3). 引數
MySQL儲存過程的引數用在儲存過程的定義,共有三種引數型別,IN,OUT,INOUT,形式如:
CREATE PROCEDURE([[IN |OUT |INOUT ] 引數名 資料類形...])
IN 輸入引數:表示該引數的值必須在呼叫儲存過程時指定,在儲存過程中修改該引數的值不能被返回,為預設值
OUT 輸出引數:該值可在儲存過程內部被改變,並可返回
INOUT 輸入輸出引數:呼叫時指定,並且可被改變和返回
Ⅰ. IN引數例子
建立:
-
mysql
> DELIMITER // <wbr><wbr><wbr><wbr> -
mysql
> CREATE<wbr><wbr> <wbr> PROCEDUREdemo_in_parameter(<wbr> INp_in int)<wbr><wbr> <wbr> -
->
<wbr> BEGIN<wbr><wbr> -
->
<wbr> SELECTp_in; <wbr><wbr><wbr> -
->
<wbr> SETp_in=2; <wbr><wbr><wbr> -
->
<wbr> SELECTp_in; <wbr><wbr><wbr> -
->
<wbr> END;<wbr><wbr> -
->
// <wbr><wbr> -
mysql
> DELIMITER ; <wbr><wbr><wbr><wbr>
執行結果:
-
mysql
> SET<wbr><wbr> @p_in=1; <wbr><wbr> -
mysql
> CALL demo_in_parameter(@p_in); <wbr><wbr><wbr><wbr> -
+------+
<wbr> -
|
p_in | <wbr><wbr><wbr> -
+------+
<wbr> -
|
1 | <wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
+------+
<wbr> -
<wbr> -
+------+
<wbr> -
|
p_in | <wbr><wbr><wbr> -
+------+
<wbr> -
|
2 | <wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
+------+
<wbr> -
<wbr> -
mysql>
<wbr> SELECT@p_in; <wbr><wbr> -
+-------+
<wbr> -
|
@p_in | <wbr><wbr><wbr> -
+-------+
<wbr> -
|
1 | <wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
+-------+
<wbr>
以上可以看出,p_in雖然在儲存過程中被修改,但並不影響@p_id的值
Ⅱ.OUT引數例子
建立:
-
mysql
> DELIMITER // <wbr><wbr><wbr><wbr> -
mysql
> CREATE<wbr><wbr> <wbr> PROCEDUREdemo_out_parameter(<wbr> OUTp_out int)<wbr><wbr> <wbr> -
->
<wbr> BEGIN<wbr> -
->
<wbr> SELECTp_out; <wbr><wbr> -
->
<wbr> SETp_out=2; <wbr><wbr> -
->
<wbr> SELECTp_out; <wbr><wbr> -
->
<wbr> END;<wbr> -
->
// <wbr><wbr> -
mysql
> DELIMITER ; <wbr><wbr><wbr><wbr>
執行結果:
-
mysql
> SET<wbr><wbr> @p_out=1; <wbr><wbr> -
mysql
> CALL sp_demo_out_parameter(@p_out); <wbr><wbr><wbr><wbr> -
+-------+
<wbr> -
|
p_out | <wbr><wbr><wbr><wbr> -
+-------+
<wbr> -
|
<wbr> NULL| <wbr><wbr><wbr><wbr> -
+-------+
<wbr> -
<wbr> -
+-------+
<wbr> -
|
p_out | <wbr><wbr><wbr> -
+-------+
<wbr> -
|
2 | <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
+-------+
<wbr> -
<wbr> -
mysql>
<wbr> SELECT@p_out; <wbr><wbr> -
+-------+
<wbr> -
|
p_out | <wbr><wbr><wbr> -
+-------+
<wbr> -
|
2 | <wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
+-------+
<wbr>
Ⅲ.
INOUT引數例子
建立:
-
mysql
> DELIMITER // <wbr><wbr><wbr><wbr> <wbr> -
mysql
> CREATE<wbr><wbr> <wbr> PROCEDUREdemo_inout_parameter(INOUT int)p_inout <wbr><wbr><wbr> <wbr> <wbr> -
->
<wbr> BEGIN<wbr> -
->
<wbr> SELECTp_inout; <wbr><wbr> -
->
<wbr> SETp_inout=2; <wbr><wbr> -
->
<wbr> SELECTp_inout; <wbr><wbr><wbr> -
->
<wbr> END;<wbr> -
->
// <wbr><wbr><wbr> -
mysql
> DELIMITER ; <wbr><wbr><wbr><wbr>
-
mysql
<wbr> >SET p_inout=1;@<wbr><wbr> <wbr> -
mysql
<wbr> >CALL demo_inout_parameter(@p_inout) ; <wbr><wbr><wbr><wbr> -
+---------+
<wbr> -
|
p_inout | <wbr><wbr><wbr> -
+---------+
<wbr> -
|
1 | <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
+---------+
<wbr> -
<wbr> -
+---------+
<wbr> -
|
p_inout | <wbr><wbr><wbr><wbr> -
+---------+
<wbr> -
|
2 | <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
+---------+
<wbr> -
<wbr> -
mysql
<wbr> >SELECT @p_inout; <wbr><wbr><wbr> -
+----------+
<wbr> -
|
@p_inout | <wbr><wbr><wbr><wbr> -
+----------+
<wbr> -
|
2 | <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
+----------+
<wbr>
(4). 變數
Ⅰ. 變數定義
DECLARE variable_name [,variable_name...] datatype [DEFAULT value];
其中,datatype為MySQL的資料型別,如:int, float, date, varchar(length)
例如:
-
DECLARE
l_int int<wbr><wbr> unsigned default<wbr><wbr> 4000000; <wbr><wbr> -
DECLARE
l_numeric DEFAULTnumber(8,2) <wbr><wbr><wbr> 9.95; <wbr><wbr> -
DECLARE
l_date date<wbr><wbr> <wbr> DEFAULT<wbr> '1999-12-31';<wbr> -
DECLARE
l_datetime DEFAULTdatetime <wbr><wbr><wbr> <wbr> '1999-12-3123:59:59'<wbr> ;<wbr> -
DECLARE
l_varchar varchar(255)<wbr><wbr> <wbr> DEFAULT<wbr> 'Thiswill ;not be padded'<wbr><wbr><wbr><wbr> <wbr><wbr>
Ⅲ. 使用者變數
ⅰ. 在MySQL客戶端使用使用者變數
-
mysql
> SELECT<wbr><wbr> <wbr> 'HelloWorld'<wbr> <wbr> into@x; <wbr><wbr> -
mysql
> SELECT<wbr><wbr> @x; <wbr><wbr> -
+-------------+
<wbr> -
|
@x | <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
+-------------+
<wbr> -
|
Hello World<wbr> | <wbr><wbr><wbr> -
+-------------+
<wbr> -
mysql
> SET<wbr><wbr> @y=<wbr> 'GoodbyeCruel ;World'<wbr><wbr> <wbr> -
mysql
> SELECT<wbr><wbr> @y; <wbr><wbr> -
+---------------------+
<wbr> -
|
@y | <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
+---------------------+
<wbr> -
|
Goodbye Cruel World | <wbr><wbr><wbr><wbr><wbr> -
+---------------------+
<wbr> -
<wbr> -
mysql
> SET<wbr><wbr> @z=1+2+3; <wbr><wbr> -
mysql
> SELECT<wbr><wbr> @z; <wbr><wbr> -
+------+
<wbr> -
|
@z | <wbr><wbr><wbr><wbr><wbr> -
+------+
<wbr> -
|
6 | <wbr><wbr><wbr><wbr><wbr><wbr> -
+------+
<wbr>
ⅱ. 在儲存過程中使用使用者變數
-
mysql
> CREATE<wbr><wbr> <wbr> PROCEDUREGreetWorld( SELECT) <wbr><wbr><wbr> CONCAT(@greeting,<wbr> 'World'<wbr> );<wbr> -
mysql
> SET<wbr><wbr> @greeting=<wbr> 'Hello';<wbr> -
mysql
> CALL GreetWorld( ); <wbr><wbr><wbr><wbr><wbr> -
+----------------------------+
<wbr> -
|
CONCAT(@greeting,<wbr> 'World'<wbr> )| <wbr><wbr> -
+----------------------------+
<wbr> -
|
Hello World | <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
+----------------------------+
<wbr>
-
mysql>
<wbr> CREATE<wbr> PROCEDUREp1() SET<wbr><wbr><wbr><wbr> @last_procedure=<wbr> 'p1';<wbr> -
mysql>
<wbr> CREATE<wbr> PROCEDUREp2() SELECT<wbr><wbr> CONCAT(<wbr> 'Lastprocedure ,@last_proc);was '<wbr><wbr><wbr> <wbr> -
mysql>
CALL p1( ); <wbr><wbr><wbr><wbr> -
mysql>
CALL p2( ); <wbr><wbr><wbr><wbr> -
+-----------------------------------------------+
<wbr> -
|
CONCAT(<wbr> 'Lastprocedure ,@last_procwas '<wbr><wbr><wbr> | <wbr><wbr> -
+-----------------------------------------------+
<wbr> -
|
<wbr> Last<wbr> procedurewas p1 | <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
+-----------------------------------------------+
<wbr>
①使用者變數名一般以@開頭
②濫用使用者變數會導致程式難以理解及管理
(5). 註釋
MySQL儲存過程可使用兩種風格的註釋
雙模槓:--
該風格一般用於單行註釋
c風格: 一般用於多行註釋
例如:
-
mysql
> DELIMITER // <wbr><wbr><wbr><wbr> -
mysql
> CREATE<wbr><wbr> <wbr> PROCEDUREproc1 --name儲存過程名<wbr><wbr> <wbr> -
->
(<wbr> INparameter1 INTEGER)<wbr><wbr> <wbr><wbr> -
->
<wbr> BEGIN<wbr><wbr> -
->
<wbr> DECLAREvariable1 CHAR(10);<wbr><wbr> <wbr><wbr> -
->
IF THENparameter1 = 17 <wbr><wbr><wbr><wbr><wbr> <wbr><wbr> -
->
<wbr> SETvariable1 'birds';= <wbr><wbr><wbr> <wbr><wbr> -
->
<wbr> ELSE<wbr> -
->
<wbr> SETvariable1 'beasts';= <wbr><wbr><wbr> <wbr><wbr> -
->
<wbr> ENDIF; <wbr><wbr><wbr> -
->
<wbr> INSERT<wbr> INTOtable1 VALUES<wbr><wbr> (variable1); <wbr><wbr> -
->
<wbr> END<wbr><wbr> -
->
// <wbr><wbr> -
mysql
> DELIMITER ; <wbr><wbr><wbr><wbr>
4.
用call和你過程名以及一個括號,括號裡面根據需要,加入引數,引數包括輸入引數、輸出引數、輸入輸出引數。具體的呼叫方法可以參看上面的例子。
5.
我們像知道一個資料庫下面有那些表,我們一般採用show tables;進行檢視。那麼我們要檢視某個資料庫下面的儲存過程,是否也可以採用呢?答案是,我們可以檢視某個資料庫下面的儲存過程,但是是令一鍾方式。
我們可以用
select name from mysql.proc where db=’資料庫名’;
或者
select routine_name from information_schema.routines where routine_schema='資料庫名';
或者
show procedure status where db='資料庫名';
進行查詢。
如果我們想知道,某個儲存過程的詳細,那我們又該怎麼做呢?是不是也可以像操作表一樣用describe 表名進行檢視呢?
答案是:我們可以檢視儲存過程的詳細,但是需要用另一種方法:
SHOW CREATE PROCEDURE 資料庫.儲存過程名;
就可以檢視當前儲存過程的詳細。
6.
ALTER PROCEDURE
更改用CREATE PROCEDURE 建立的預先指定的儲存過程,其不會影響相關儲存過程或儲存功能。
7.
刪除一個儲存過程比較簡單,和刪除表一樣:
DROP PROCEDURE
從MySQL的表格中刪除一個或多個儲存過程。
8.
(1). 變數作用域
內部的變數在其作用域範圍內享有更高的優先權,當執行到end。變數時,內部變數消失,此時已經在其作用域外,變數不再可見了,應為在儲存
過程外再也不能找到這個申明的變數,但是你可以通過out引數或者將其值指派
給會話變數來儲存其值。
-
mysql
> DELIMITER // <wbr><wbr><wbr><wbr> -
mysql
> CREATE<wbr><wbr> <wbr> PROCEDUREproc3() <wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> x1 varchar(5)<wbr><wbr> <wbr> default<wbr> 'outer';<wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> x1 varchar(5)<wbr><wbr> <wbr> default<wbr> 'inner';<wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> x1; <wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> x1; <wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> -
-> // <wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
mysql
> DELIMITER ; <wbr><wbr><wbr><wbr>
Ⅰ. if-then -else語句
-
mysql
> DELIMITER // <wbr><wbr><wbr><wbr> -
mysql
> CREATE<wbr><wbr> <wbr> PROCEDUREproc2(<wbr> INparameter int)<wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> var int;<wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> var=parameter+1; <wbr><wbr> -
-> if var=0 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> intot values(17);<wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> if; <wbr><wbr> -
-> if parameter=0 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> t set<wbr><wbr> s1=s1+1; <wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> t set<wbr><wbr> s1=s1+2; <wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> if; <wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> -
-> // <wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
mysql
> DELIMITER ; <wbr><wbr><wbr><wbr>
Ⅱ. case語句:
-
mysql
> DELIMITER // <wbr><wbr><wbr><wbr> -
mysql
> CREATE<wbr><wbr> <wbr> PROCEDUREproc3 in(<wbr><wbr> parameter int)<wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> var int;<wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> var=parameter+1; <wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> var <wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> 0 then<wbr><wbr> <wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> intot values(17);<wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> 1 then<wbr><wbr> <wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> intot values(18);<wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> intot values(19);<wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> case;<wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> -
-> // <wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
mysql
> DELIMITER ; <wbr><wbr><wbr><wbr>
(3). 迴圈語句
Ⅰ. while ···· end while:
-
mysql
> DELIMITER // <wbr><wbr><wbr><wbr> -
mysql
> CREATE<wbr><wbr> <wbr> PROCEDUREproc4() <wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> var int;<wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> var=0; <wbr><wbr> -
-> while var<6 do <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> intot values(var);<wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> var=var+1; <wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> while; <wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> -
-> // <wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
mysql
> DELIMITER ; <wbr><wbr><wbr><wbr>
Ⅱ. repeat···· end repeat:
它在執行操作後檢查結果,而while則是執行前進行檢查。
-
mysql
> DELIMITER // <wbr><wbr><wbr><wbr> -
mysql
> CREATE<wbr><wbr> <wbr> PROCEDUREproc5 () <wbr><wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> v int;<wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> v=0; <wbr><wbr> -
-> repeat <wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> intot values(v);<wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> v=v+1; <wbr><wbr> -
-> until v>=5 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> repeat; <wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> -
-> // <wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
mysql
> DELIMITER ; <wbr><wbr><wbr><wbr>
Ⅲ. loop ·····end
loop:
loop迴圈不需要初始條件,這點和while 迴圈相似,同時和repeat迴圈一樣不需要結束條件,
leave語句的意義是離開迴圈。
-
mysql
> DELIMITER // <wbr><wbr><wbr><wbr> -
mysql
> CREATE<wbr><wbr> <wbr> PROCEDUREproc6 () <wbr><wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> v int;<wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> v=0; <wbr><wbr> -
-> LOOP_LABLE:loop <wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> intot values(v);<wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> v=v+1; <wbr><wbr> -
-> if v >=5 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> <wbr> -
-> leave LOOP_LABLE; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> if; <wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> loop; <wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> -
-> // <wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
mysql
> DELIMITER ; <wbr><wbr><wbr><wbr>
Ⅳ. LABLES 標號:
標號可以用在begin repeat while 或者loop 語句前,語句標號只能在合法的語句前面使用。可以跳出迴圈,使執行指令達到複合語句的最後一步。
(4). ITERATE迭代
Ⅰ. ITERATE:
通過引用複合語句的標號,來從新開始複合語句
-
mysql
> DELIMITER // <wbr><wbr><wbr><wbr> -
mysql
> CREATE<wbr><wbr> <wbr> PROCEDUREproc10 () <wbr><wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> v int;<wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> v=0; <wbr><wbr> -
-> LOOP_LABLE:loop <wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
-> if v=3 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> <wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> v=v+1; <wbr><wbr> -
-> ITERATE LOOP_LABLE; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> if; <wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> intot values(v);<wbr><wbr> <wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> v=v+1; <wbr><wbr> -
-> if v>=5 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> <wbr> -
-> leave LOOP_LABLE; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> if; <wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> loop; <wbr><wbr> -
-> <wbr><wbr><wbr><wbr><wbr><wbr> <wbr> -
-> // <wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
mysql
> DELIMITER ; <wbr><wbr><wbr><wbr>
9.
(1).字串類
CHARSET(str) //返回字串字符集
CONCAT (string2 [,... ]) //連線字串
INSTR (string ,substring ) //返回substring首次在string中出現的位置,不存在返回0
LCASE (string2 ) //轉換成小寫
LEFT (string2 ,length ) //從string2中的左邊起取length個字元
LENGTH (string ) //string長度
LOAD_FILE (file_name ) //從檔案讀取內容
LOCATE (substring , string [,start_position ] ) 同INSTR,但可指定開始位置
LPAD (string2 ,length ,pad ) //重複用pad加在string開頭,直到字串長度為length
LTRIM (string2 ) //去除前端空格
REPEAT (string2 ,count ) //重複count次
REPLACE (str ,search_str ,replace_str ) //在str中用replace_str替換search_str
RPAD (string2 ,length ,pad) //在str後用pad補充,直到長度為length
RTRIM (string2 ) //去除後端空格
STRCMP (string1 ,string2 ) //逐字元比較兩字串大小,
SUBSTRING (str , position [,length ]) //從str的position開始,取length個字元,
注:mysql中處理字串時,預設第一個字元下標為1,即引數position必須大於等於1
-
mysql>
<wbr> select<wbr> substring('abcd',0,2);<wbr> -
+-----------------------+
<wbr> -
|
<wbr> substring('abcd',0,2)| <wbr><wbr> -
+-----------------------+
<wbr> -
|
| <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
+-----------------------+
<wbr> -
1
row in<wbr><wbr> <wbr> set(0.00 sec) <wbr><wbr><wbr> -
<wbr> -
mysql>
<wbr> select<wbr> substring('abcd',1,2);<wbr> -
+-----------------------+
<wbr> -
|
<wbr> substring('abcd',1,2)| <wbr><wbr> -
+-----------------------+
<wbr> -
|
ab | <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
+-----------------------+
<wbr> -
1
row in<wbr><wbr> <wbr> set(0.02 sec) <wbr><wbr><wbr>
TRIM([[BOTH|LEADING|TRAILING] [padding] FROM]string2)
//去除指定位置的指定字元
UCASE (string2 ) //轉換成大寫
RIGHT(string2,length) //取string2最後length個字元
SPACE(count) //生成count個空格
(2).數學類
ABS (number2 ) //絕對值
BIN (decimal_number ) //十進位制轉二進位制
CEILING (number2 ) //向上取整
CONV(number2,from_base,to_base) //進位制轉換
FLOOR (number2 ) //向下取整
FORMAT (number,decimal_places ) //保留小數位數
HEX (DecimalNumber ) //轉十六進位制
注:HEX()中可傳入字串,則返回其ASC-11碼,如HEX('DEF')返回4142143
也可以傳入十進位制整數,返回其十六進位制編碼,如HEX(25)返回19
LEAST (number , number2 [,..]) //求最小值
MOD (numerator ,denominator ) //求餘
POWER (number ,power ) //求指數
RAND([seed]) //隨機數
ROUND (number [,decimals ]) //四捨五入,decimals為小數位數]
注:返回型別並非均為整數,如:
(1)預設變為整形值
-
mysql>
<wbr> selectround(1.23); <wbr><wbr> -
+-------------+
<wbr> -
|
round(1.23) | <wbr><wbr><wbr> -
+-------------+
<wbr> -
|
1 | <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
+-------------+
<wbr> -
1
row in<wbr><wbr> <wbr> set(0.00 sec) <wbr><wbr><wbr> -
<wbr> -
mysql>
<wbr> selectround(1.56); <wbr><wbr> -
+-------------+
<wbr> -
|
round(1.56) | <wbr><wbr><wbr> -
+-------------+
<wbr> -
|
2 | <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
+-------------+
<wbr> -
1
row in<wbr><wbr> <wbr> set(0.00 sec) <wbr><wbr><wbr>
(2)可以設定小數位數,返回浮點型資料
-
mysql>
<wbr> selectround(1.567,2); <wbr><wbr> -
+----------------+
<wbr> -
|
round(1.567,2) | <wbr><wbr><wbr> -
+----------------+
<wbr> -
|
1.57 | <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> -
+----------------+
<wbr> -
1
row in<wbr><wbr> <wbr> set(0.00 sec) <wbr><wbr><wbr>
SIGN (number2 ) //
(3).日期時間類
ADDTIME (date2 ,time_interval ) //將time_interval加到date2CONVERT_TZ (datetime2 ,fromTZ ,toTZ ) //轉換時區
CURRENT_DATE ( ) //當前日期
CURRENT_TIME ( ) //當前時間
CURRENT_TIMESTAMP ( ) //當前時間戳
DATE (datetime ) //返回datetime的日期部分
DATE_ADD (date2 , INTERVAL d_value d_type ) //在date2中加上日期或時間
DATE_FORMAT (datetime ,FormatCodes ) //使用formatcodes格式顯示datetime
DATE_SUB (date2 , INTERVAL d_value d_type ) //在date2上減去一個時間
DATEDIFF (date1 ,date2 ) //兩個日期差
DAY (date ) //返回日期的天
DAYNAME (date ) //英文星期
DAYOFWEEK (date ) //星期(1-7) ,1為星期天
DAYOFYEAR (date ) //一年中的第幾天
EXTRACT (interval_name FROM date ) //從date中提取日期的指定部分
MAKEDATE (year ,day ) //給出年及年中的第幾天,生成日期串
MAKETIME (hour ,minute ,second ) //生成時間串
MONTHNAME (date ) //英文月份名
NOW ( ) //當前時間
SEC_TO_TIME (seconds ) //秒數轉成時間
STR_TO_DATE (string ,format ) //字串轉成時間,以format格式顯示
TIMEDIFF (datetime1 ,datetime2 ) //兩個時間差
TIME_TO_SEC (time ) //時間轉秒數]
WEEK (date_time [,start_of_week ]) //第幾周
YEAR (datetime ) //年份
DAYOFMONTH(datetime) //月的第幾天
HOUR(datetime) //小時
LAST_DAY(date) //date的月的最後日期
MICROSECOND(datetime) //微秒
MONTH(datetime) //月
MINUTE(datetime) //分返回符號,正負或0
SQRT(number2) //開平方
相關文章
- mongo 儲存過程詳解Go儲存過程
- mysql 儲存過程MySql儲存過程
- MySQL---------儲存過程MySql儲存過程
- mysql儲存過程整理MySql儲存過程
- MySQL之儲存過程MySql儲存過程
- 深入mysql建立自定義函式與儲存過程的詳解MySql函式儲存過程
- mysql如何呼叫儲存過程MySql儲存過程
- Mysql 儲存過程的使用MySql儲存過程
- 詳解MySQL儲存過程引數有三種型別(in、out、inout)MySql儲存過程型別
- mySql 儲存過程與函式MySql儲存過程函式
- MySQL 儲存過程和函式MySql儲存過程函式
- mysql儲存過程的引數MySql儲存過程
- Laravel 中使用 MySQL 儲存過程LaravelMySql儲存過程
- MySql資料庫——儲存過程MySql資料庫儲存過程
- MySQL儲存過程和函式MySql儲存過程函式
- MySQL儲存過程 (即函式)MySql儲存過程函式
- mysql和orcale的儲存過程和儲存函式MySql儲存過程儲存函式
- mysql 儲存過程 procedure 批次建表MySql儲存過程
- MySQL儲存過程中如何使用ROLLBACKMySql儲存過程
- Mysql 5.7儲存過程的學習MySql儲存過程
- MySQL--儲存過程與檢視MySql儲存過程
- MySQL儲存過程的建立和使用MySql儲存過程
- MySQL儲存過程語句及呼叫MySql儲存過程
- MySQL入門--儲存過程(PROCEDURE)和儲存函式(FUNCTION)MySql儲存過程儲存函式Function
- MySQL入門系列:儲存程式(三)之儲存過程簡介MySql儲存過程
- 23. 使用MySQL之使用儲存過程MySql儲存過程
- MySQL的寫入資料儲存過程MySql儲存過程
- mysql檢視儲存過程show procedure status;MySql儲存過程
- MySQL 儲存過程進行切換表MySql儲存過程
- [MySQL光速入門]015 聊聊儲存過程MySql儲存過程
- mysql多次呼叫儲存過程的問題MySql儲存過程
- MySQL全面瓦解16:儲存過程相關MySql儲存過程
- MySQL自定義函式與儲存過程MySql函式儲存過程
- Mysql儲存過程基礎(案例+程式碼)MySql儲存過程
- Mysql儲存過程中使用多遊標MySql儲存過程
- MySQL-09-mysql 儲存過程入門介紹MySql儲存過程
- Oracle儲存過程乾貨(一):儲存過程基礎Oracle儲存過程
- SQL 儲存過程裡呼叫另一個儲存過程SQL儲存過程
- 儲存過程與儲存函式儲存過程儲存函式