mysql儲存過程詳解

鴨脖發表於2015-07-11

1.     <wbr><wbr><wbr><wbr><wbr> 儲存過程簡介

 <wbr>

我們常用的運算元據庫語言SQL語句在執行的時候需要要先編譯,然後執行,而儲存過程(Stored Procedure)是一組為了完成特定功能的SQL語句集,經編譯後儲存在資料庫中,使用者通過指定儲存過程的名字並給定引數(如果該儲存過程帶有引數)來呼叫執行它。

一個儲存過程是一個可程式設計的函式,它在資料庫中建立並儲存。它可以有SQL語句和一些特殊的控制結構組成。當希望在不同的應用程式或平臺上執行相同的函式,或者封裝特定功能時,儲存過程是非常有用的。資料庫中的儲存過程可以看做是對程式設計中物件導向方法的模擬。它允許控制資料的訪問方式。

儲存過程通常有以下優點:

(1).儲存過程增強了SQL語言的功能和靈活性。儲存過程可以用流控制語句編寫,有很強的靈活性,可以完成複雜的判斷和較複雜的運算。

(2).儲存過程允許標準元件是程式設計。儲存過程被建立後,可以在程式中被多次呼叫,而不必重新編寫該儲存過程的SQL語句。而且資料庫專業人員可以隨時對儲存過程進行修改,對應用程式原始碼毫無影響。

(3).儲存過程能實現較快的執行速度。如果某一操作包含大量的Transaction-SQL程式碼或分別被多次執行,那麼儲存過程要比批處理的執行速度快很多。因為儲存過程是預編譯的。在首次執行一個儲存過程時查詢,優化器對其進行分析優化,並且給出最終被儲存在系統表中的執行計劃。而批處理的Transaction-SQL語句在每次執行時都要進行編譯和優化,速度相對要慢一些。

(4).儲存過程能過減少網路流量。針對同一個資料庫物件的操作(如查詢、修改),如果這一操作所涉及的Transaction-SQL語句被組織程儲存過程,那麼當在客戶計算機上呼叫該儲存過程時,網路中傳送的只是該呼叫語句,從而大大增加了網路流量並降低了網路負載。

(5).儲存過程可被作為一種安全機制來充分利用。系統管理員通過執行某一儲存過程的許可權進行限制,能夠實現對相應的資料的訪問許可權的限制,避免了非授權使用者對資料的訪問,保證了資料的安全。

 <wbr>

2.     <wbr><wbr><wbr><wbr><wbr> 關於MySQL的儲存過程

儲存過程是資料庫儲存的一個重要的功能,但是MySQL5.0以前並不支援儲存過程,這使得MySQL在應用上大打折扣。好在MySQL 5.0終於開始已經支援儲存過程,這樣即可以大大提高資料庫的處理速度,同時也可以提高資料庫程式設計的靈活性。

3.     <wbr><wbr><wbr><wbr><wbr> MySQL儲存過程的建立

 <wbr>

(1). 格式

MySQL儲存過程建立的格式:CREATE PROCEDURE 過程名 ([過程引數[,...]])
[
特性 ...] 過程體

這裡先舉個例子:
   
<wbr><wbr><wbr>

  1. mysql> DELIMITER //  <wbr><wbr><wbr>
  2. mysql> <wbr>CREATE <wbr>PROCEDURE proc1(<wbr>OUT <wbr><wbr>int)  <wbr>
  3.     -> <wbr><wbr><wbr><wbr><wbr>BEGIN <wbr>
  4.     -> <wbr><wbr><wbr><wbr><wbr>SELECT <wbr>COUNT(*) <wbr>INTO <wbr><wbr>FROM <wbr>user;  <wbr>
  5.     -> <wbr><wbr><wbr><wbr><wbr>END <wbr>
  6.     -> //  <wbr><wbr><wbr><wbr><wbr><wbr>
  7. mysql> DELIMITER <wbr><wbr><wbr>
 <wbr>
注:

1)這裡需要注意的是DELIMITER //DELIMITER ;兩句,DELIMITER是分割符的意思,因為MySQL預設以";"為分隔符,如果我們沒有宣告分割符,那麼編譯器會把儲存過程當成SQL語句進行處理,則儲存過程的編譯過程會報錯,所以要事先用DELIMITER關鍵字申明當前段分隔符,這樣MySQL才會將";"當做儲存過程中的程式碼,不會執行這些程式碼,用完了之後要把分隔符還原。

2)儲存過程根據需要可能會有輸入、輸出、輸入輸出引數,這裡有一個輸出引數s,型別是int型,如果有多個引數用","分割開。

3)過程體的開始與結束使用BEGINEND進行標識。

這樣,我們的一個MySQL儲存過程就完成了,是不是很容易呢?看不懂也沒關係,接下來,我們詳細的講解。

 <wbr>

(2). 宣告分割符

 <wbr>

其實,關於宣告分割符,上面的註解已經寫得很清楚,不需要多說,只是稍微要注意一點的是:如果是用MySQLAdministrator管理工具時,可以直接建立,不再需要宣告。

 <wbr>

(3). 引數

MySQL儲存過程的引數用在儲存過程的定義,共有三種引數型別,IN,OUT,INOUT,形式如:

CREATE PROCEDURE([[IN |OUT |INOUT ] 引數名 資料類形...])

IN 輸入引數:表示該引數的值必須在呼叫儲存過程時指定,在儲存過程中修改該引數的值不能被返回,為預設值

OUT 輸出引數:該值可在儲存過程內部被改變,並可返回

INOUT 輸入輸出引數:呼叫時指定,並且可被改變和返回

. IN引數例子

建立:

  1. mysql DELIMITER //  <wbr><wbr><wbr><wbr>
  2. mysql <wbr><wbr>CREATE <wbr>PROCEDURE demo_in_parameter(<wbr>IN p_in <wbr><wbr>int)  <wbr>
  3. -> <wbr>BEGIN   <wbr><wbr>
  4. -> <wbr>SELECT p_in;   <wbr><wbr><wbr>
  5. -> <wbr>SET p_in=2;   <wbr><wbr><wbr>
  6. -> <wbr>SELECT p_in;   <wbr><wbr><wbr>
  7. -> <wbr>END  <wbr><wbr>
  8. -> //  <wbr><wbr>
  9. mysql DELIMITER <wbr><wbr><wbr><wbr>


執行結果
:

  1. mysql <wbr><wbr>SET @p_in=1;  <wbr><wbr>
  2. mysql CALL demo_in_parameter(@p_in);  <wbr><wbr><wbr><wbr>
  3. +------+  <wbr>
  4. p_in |  <wbr><wbr><wbr>
  5. +------+  <wbr>
  6.      <wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  7. +------+  <wbr>
  8.  <wbr>
  9. +------+  <wbr>
  10. p_in |  <wbr><wbr><wbr>
  11. +------+  <wbr>
  12.      <wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  13. +------+  <wbr>
  14.  <wbr>
  15. mysql> <wbr>SELECT @p_in;  <wbr><wbr>
  16. +-------+  <wbr>
  17. @p_in |  <wbr><wbr><wbr>
  18. +-------+  <wbr>
  19.     |  <wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  20. +-------+  <wbr>


以上可以看出,
p_in雖然在儲存過程中被修改,但並不影響@p_id的值

 <wbr>

.OUT引數例子

建立:

  1. mysql DELIMITER //  <wbr><wbr><wbr><wbr>
  2. mysql <wbr><wbr>CREATE <wbr>PROCEDURE demo_out_parameter(<wbr>OUT p_out <wbr><wbr>int)  <wbr>
  3. -> <wbr>BEGIN <wbr>
  4. -> <wbr>SELECT p_out;  <wbr><wbr>
  5. -> <wbr>SET p_out=2;  <wbr><wbr>
  6. -> <wbr>SELECT p_out;  <wbr><wbr>
  7. -> <wbr>END;  <wbr>
  8. -> //  <wbr><wbr>
  9. mysql DELIMITER <wbr><wbr><wbr><wbr>


執行結果
:

  1. mysql <wbr><wbr>SET @p_out=1;  <wbr><wbr>
  2. mysql CALL sp_demo_out_parameter(@p_out);  <wbr><wbr><wbr><wbr>
  3. +-------+  <wbr>
  4. p_out   <wbr><wbr><wbr><wbr>
  5. +-------+  <wbr>
  6. <wbr>NULL    <wbr><wbr><wbr><wbr>
  7. +-------+  <wbr>
  8.  <wbr>
  9. +-------+  <wbr>
  10. p_out |  <wbr><wbr><wbr>
  11. +-------+  <wbr>
  12.       <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  13. +-------+  <wbr>
  14.  <wbr>
  15. mysql> <wbr>SELECT @p_out;  <wbr><wbr>
  16. +-------+  <wbr>
  17. p_out |  <wbr><wbr><wbr>
  18. +-------+  <wbr>
  19.     |  <wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  20. +-------+  <wbr>


. INOUT引數例子

建立:

  1. mysql DELIMITER //  <wbr><wbr><wbr><wbr> <wbr>
  2. mysql <wbr><wbr>CREATE <wbr>PROCEDURE demo_inout_parameter(INOUT p_inout <wbr><wbr><wbr>int)  <wbr> <wbr>
  3. -> <wbr>BEGIN <wbr>
  4. -> <wbr>SELECT p_inout;  <wbr><wbr>
  5. -> <wbr>SET p_inout=2;  <wbr><wbr>
  6. -> <wbr>SELECT p_inout;   <wbr><wbr><wbr>
  7. -> <wbr>END;  <wbr>
  8. -> //   <wbr><wbr><wbr>
  9. mysql DELIMITER <wbr><wbr><wbr><wbr>

 <wbr>

 <wbr>

執行結果:
  1. mysql <wbr>> SET @<wbr><wbr>p_inout=1;  <wbr>
  2. mysql <wbr>> CALL demo_inout_parameter(@p_inout) ;  <wbr><wbr><wbr><wbr>
  3. +---------+  <wbr>
  4. p_inout |  <wbr><wbr><wbr>
  5. +---------+  <wbr>
  6.       |  <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  7. +---------+  <wbr>
  8.  <wbr>
  9. +---------+  <wbr>
  10. p_inout   <wbr><wbr><wbr><wbr>
  11. +---------+  <wbr>
  12.       |  <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  13. +---------+  <wbr>
  14.  <wbr>
  15. mysql <wbr>> SELECT @p_inout;  <wbr><wbr><wbr>
  16. +----------+  <wbr>
  17. @p_inout   <wbr><wbr><wbr><wbr>
  18. +----------+  <wbr>
  19.        |  <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  20. +----------+ <wbr>

(4). 變數

. 變數定義

DECLARE variable_name [,variable_name...] datatype [DEFAULT value];

其中,datatypeMySQL的資料型別,如:int, float, date, varchar(length)

例如:

  1. DECLARE l_int <wbr><wbr>int unsigned <wbr><wbr>default 4000000;  <wbr><wbr>
  2. DECLARE l_numeric number(8,2) <wbr><wbr><wbr>DEFAULT 9.95;  <wbr><wbr>
  3. DECLARE l_date <wbr><wbr>date <wbr>DEFAULT <wbr>'1999-12-31';  <wbr>
  4. DECLARE l_datetime datetime <wbr><wbr><wbr>DEFAULT <wbr>'1999-12-31 23:59:59'<wbr>;  <wbr>
  5. DECLARE l_varchar <wbr><wbr>varchar(255) <wbr>DEFAULT <wbr>'This will not be padded'<wbr><wbr><wbr><wbr>  <wbr><wbr>

 <wbr>

 <wbr>

. 變數賦值

 SET 變數名 = 表示式值 [,variable_name = expression ...]<wbr><wbr>

 <wbr>

. 使用者變數

 <wbr>

. MySQL客戶端使用使用者變數

  1. mysql <wbr><wbr>SELECT <wbr>'Hello World'<wbr> <wbr>into @x;  <wbr><wbr>
  2. mysql <wbr><wbr>SELECT @x;  <wbr><wbr>
  3. +-------------+  <wbr>
  4.   @x        |  <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  5. +-------------+  <wbr>
  6. Hello World<wbr> |  <wbr><wbr><wbr>
  7. +-------------+  <wbr>
  8. mysql <wbr><wbr>SET @y=<wbr>'Goodbye Cruel World'<wbr><wbr>;  <wbr>
  9. mysql <wbr><wbr>SELECT @y;  <wbr><wbr>
  10. +---------------------+  <wbr>
  11.     @y              |  <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  12. +---------------------+  <wbr>
  13. Goodbye Cruel World |  <wbr><wbr><wbr><wbr><wbr>
  14. +---------------------+  <wbr>
  15.  <wbr>
  16. mysql <wbr><wbr>SET @z=1+2+3;  <wbr><wbr>
  17. mysql <wbr><wbr>SELECT @z;  <wbr><wbr>
  18. +------+  <wbr>
  19. @z   |  <wbr><wbr><wbr><wbr><wbr>
  20. +------+  <wbr>
  21.    |  <wbr><wbr><wbr><wbr><wbr><wbr>
  22. +------+  <wbr>

ⅱ. 在儲存過程中使用使用者變數

  1. mysql <wbr><wbr>CREATE <wbr>PROCEDURE GreetWorld( <wbr><wbr><wbr>SELECT CONCAT(@greeting,<wbr>World'<wbr>);  <wbr>
  2. mysql <wbr><wbr>SET @greeting=<wbr>'Hello';  <wbr>
  3. mysql CALL GreetWorld( );  <wbr><wbr><wbr><wbr><wbr>
  4. +----------------------------+  <wbr>
  5. CONCAT(@greeting,<wbr>World'<wbr>|  <wbr><wbr>
  6. +----------------------------+  <wbr>
  7.  Hello World               |  <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  8. +----------------------------+  <wbr>

 <wbr>

. 在儲存過程間傳遞全域性範圍的使用者變數
  1. mysql> <wbr>CREATE <wbr>PROCEDURE p1()   <wbr><wbr><wbr><wbr>SET @last_procedure=<wbr>'p1';  <wbr>
  2. mysql> <wbr>CREATE <wbr>PROCEDURE p2() <wbr><wbr>SELECT CONCAT(<wbr>'Last procedure was '<wbr><wbr><wbr>,@last_proc);  <wbr>
  3. mysql> CALL p1( );  <wbr><wbr><wbr><wbr>
  4. mysql> CALL p2( );  <wbr><wbr><wbr><wbr>
  5. +-----------------------------------------------+  <wbr>
  6. CONCAT(<wbr>'Last procedure was '<wbr><wbr><wbr>,@last_proc  |  <wbr><wbr>
  7. +-----------------------------------------------+  <wbr>
  8. <wbr>Last <wbr>procedure was 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>
  9. +-----------------------------------------------+  <wbr>

 <wbr>

 <wbr>

注意:

使用者變數名一般以@開頭

濫用使用者變數會導致程式難以理解及管理

 <wbr>

(5). 註釋

 <wbr>

MySQL儲存過程可使用兩種風格的註釋

雙模槓:--

該風格一般用於單行註釋

c風格: 一般用於多行註釋

例如:

 <wbr>

  1. mysql DELIMITER //  <wbr><wbr><wbr><wbr>
  2. mysql <wbr><wbr>CREATE <wbr>PROCEDURE proc1 <wbr><wbr>--name儲存過程名  <wbr>
  3. -> (<wbr>IN parameter1 <wbr><wbr>INTEGER  <wbr><wbr>
  4. -> <wbr>BEGIN   <wbr><wbr>
  5. -> <wbr>DECLARE variable1 <wbr><wbr>CHAR(10);   <wbr><wbr>
  6. -> IF parameter1 17 <wbr><wbr><wbr><wbr><wbr>THEN   <wbr><wbr>
  7. -> <wbr>SET variable1 <wbr><wbr><wbr>'birds'  <wbr><wbr>
  8. -> <wbr>ELSE <wbr>
  9. -> <wbr>SET variable1 <wbr><wbr><wbr>'beasts'  <wbr><wbr>
  10. -> <wbr>END IF;   <wbr><wbr><wbr>
  11. -> <wbr>INSERT <wbr>INTO table1 <wbr><wbr>VALUES (variable1);  <wbr><wbr>
  12. -> <wbr>END   <wbr><wbr>
  13. -> //  <wbr><wbr>
  14. mysql DELIMITER ;  <wbr><wbr><wbr><wbr>

 <wbr>

4.     <wbr><wbr><wbr><wbr><wbr> MySQL儲存過程的呼叫

call和你過程名以及一個括號,括號裡面根據需要,加入引數,引數包括輸入引數、輸出引數、輸入輸出引數。具體的呼叫方法可以參看上面的例子。

5.     <wbr><wbr><wbr><wbr><wbr> MySQL儲存過程的查詢

我們像知道一個資料庫下面有那些表,我們一般採用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 資料庫.儲存過程名;

就可以檢視當前儲存過程的詳細。

 <wbr>

6.     <wbr><wbr><wbr><wbr><wbr> MySQL儲存過程的修改

ALTER PROCEDURE

更改用CREATE PROCEDURE 建立的預先指定的儲存過程,其不會影響相關儲存過程或儲存功能。

 <wbr>

7.     <wbr><wbr><wbr><wbr><wbr> MySQL儲存過程的刪除

刪除一個儲存過程比較簡單,和刪除表一樣:

DROP PROCEDURE

MySQL的表格中刪除一個或多個儲存過程。

 <wbr>

8.     <wbr><wbr><wbr><wbr><wbr> MySQL儲存過程的控制語句

(1). 變數作用域

內部的變數在其作用域範圍內享有更高的優先權,當執行到end。變數時,內部變數消失,此時已經在其作用域外,變數不再可見了,應為在儲存
過程外再也不能找到這個申明的變數,但是你可以通過out引數或者將其值指派
給會話變數來儲存其值。

 <wbr>

 <wbr>

  1. mysql DELIMITER //  <wbr><wbr><wbr><wbr>
  2. mysql <wbr><wbr>CREATE <wbr>PROCEDURE proc3()  <wbr><wbr>
  3.      -> <wbr><wbr><wbr><wbr><wbr><wbr>begin <wbr>
  4.      -> <wbr><wbr><wbr><wbr><wbr><wbr>declare x1 <wbr><wbr>varchar(5) <wbr>default <wbr>'outer';  <wbr>
  5.      -> <wbr><wbr><wbr><wbr><wbr><wbr>begin <wbr>
  6.      -> <wbr><wbr><wbr><wbr><wbr><wbr>declare x1 <wbr><wbr>varchar(5) <wbr>default <wbr>'inner';  <wbr>
  7.      -> <wbr><wbr><wbr><wbr><wbr><wbr>select x1;  <wbr><wbr>
  8.      -> <wbr><wbr><wbr><wbr><wbr><wbr>end;  <wbr>
  9.      -> <wbr><wbr><wbr><wbr><wbr><wbr>select x1;  <wbr><wbr>
  10.      -> <wbr><wbr><wbr><wbr><wbr><wbr>end;  <wbr>
  11.      -> //  <wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  12. mysql DELIMITER ;  <wbr><wbr><wbr><wbr>

 <wbr>

 <wbr>(2). 條件語句

. if-then -else語句

 <wbr>

 <wbr>

 <wbr>

  1. mysql DELIMITER //  <wbr><wbr><wbr><wbr>
  2. mysql <wbr><wbr>CREATE <wbr>PROCEDURE proc2(<wbr>IN parameter <wbr><wbr>int)  <wbr>
  3.      -> <wbr><wbr><wbr><wbr><wbr><wbr>begin <wbr>
  4.      -> <wbr><wbr><wbr><wbr><wbr><wbr>declare var <wbr><wbr>int;  <wbr>
  5.      -> <wbr><wbr><wbr><wbr><wbr><wbr>set var=parameter+1;  <wbr><wbr>
  6.      -> if var=0 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>then <wbr>
  7.      -> <wbr><wbr><wbr><wbr><wbr><wbr>insert <wbr>into <wbr><wbr>values(17);  <wbr>
  8.      -> <wbr><wbr><wbr><wbr><wbr><wbr>end if;  <wbr><wbr>
  9.      -> if parameter=0 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>then <wbr>
  10.      -> <wbr><wbr><wbr><wbr><wbr><wbr>update <wbr><wbr>set s1=s1+1;  <wbr><wbr>
  11.      -> <wbr><wbr><wbr><wbr><wbr><wbr>else <wbr>
  12.      -> <wbr><wbr><wbr><wbr><wbr><wbr>update <wbr><wbr>set s1=s1+2;  <wbr><wbr>
  13.      -> <wbr><wbr><wbr><wbr><wbr><wbr>end if;  <wbr><wbr>
  14.      -> <wbr><wbr><wbr><wbr><wbr><wbr>end;  <wbr>
  15.      -> //  <wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  16. mysql DELIMITER ;  <wbr><wbr><wbr><wbr>


. case語句: 

  1. mysql DELIMITER //  <wbr><wbr><wbr><wbr>
  2. mysql <wbr><wbr>CREATE <wbr>PROCEDURE proc3 (<wbr><wbr>in parameter <wbr><wbr>int)  <wbr>
  3.      -> <wbr><wbr><wbr><wbr><wbr><wbr>begin <wbr>
  4.      -> <wbr><wbr><wbr><wbr><wbr><wbr>declare var <wbr><wbr>int;  <wbr>
  5.      -> <wbr><wbr><wbr><wbr><wbr><wbr>set var=parameter+1;  <wbr><wbr>
  6.      -> <wbr><wbr><wbr><wbr><wbr><wbr>case var  <wbr><wbr>
  7.      -> <wbr><wbr><wbr><wbr><wbr><wbr>when <wbr><wbr>then   <wbr><wbr>
  8.      -> <wbr><wbr><wbr><wbr><wbr><wbr>insert <wbr>into <wbr><wbr>values(17);  <wbr>
  9.      -> <wbr><wbr><wbr><wbr><wbr><wbr>when <wbr><wbr>then   <wbr><wbr>
  10.      -> <wbr><wbr><wbr><wbr><wbr><wbr>insert <wbr>into <wbr><wbr>values(18);  <wbr>
  11.      -> <wbr><wbr><wbr><wbr><wbr><wbr>else   <wbr><wbr>
  12.      -> <wbr><wbr><wbr><wbr><wbr><wbr>insert <wbr>into <wbr><wbr>values(19);  <wbr>
  13.      -> <wbr><wbr><wbr><wbr><wbr><wbr>end <wbr>case;  <wbr>
  14.      -> <wbr><wbr><wbr><wbr><wbr><wbr>end;  <wbr>
  15.      -> //  <wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  16. mysql DELIMITER <wbr><wbr><wbr><wbr>

 <wbr>

(3). 迴圈語句

. while ···· end while

  1. mysql DELIMITER //  <wbr><wbr><wbr><wbr>
  2. mysql <wbr><wbr>CREATE <wbr>PROCEDURE proc4()  <wbr><wbr>
  3.      -> <wbr><wbr><wbr><wbr><wbr><wbr>begin <wbr>
  4.      -> <wbr><wbr><wbr><wbr><wbr><wbr>declare var <wbr><wbr>int;  <wbr>
  5.      -> <wbr><wbr><wbr><wbr><wbr><wbr>set var=0;  <wbr><wbr>
  6.      -> while var<6 do  <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  7.      -> <wbr><wbr><wbr><wbr><wbr><wbr>insert <wbr>into <wbr><wbr>values(var);  <wbr>
  8.      -> <wbr><wbr><wbr><wbr><wbr><wbr>set var=var+1;  <wbr><wbr>
  9.      -> <wbr><wbr><wbr><wbr><wbr><wbr>end while;  <wbr><wbr>
  10.      -> <wbr><wbr><wbr><wbr><wbr><wbr>end;  <wbr>
  11.      -> //  <wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  12. mysql DELIMITER <wbr><wbr><wbr><wbr>

 <wbr>

 <wbr>

. repeat···· end repeat

它在執行操作後檢查結果,而while則是執行前進行檢查。

  1. mysql DELIMITER //  <wbr><wbr><wbr><wbr>
  2. mysql <wbr><wbr>CREATE <wbr>PROCEDURE proc5 ()  <wbr><wbr><wbr>
  3.      -> <wbr><wbr><wbr><wbr><wbr><wbr>begin   <wbr><wbr>
  4.      -> <wbr><wbr><wbr><wbr><wbr><wbr>declare <wbr><wbr>int;  <wbr>
  5.      -> <wbr><wbr><wbr><wbr><wbr><wbr>set v=0;  <wbr><wbr>
  6.      -> repeat  <wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  7.      -> <wbr><wbr><wbr><wbr><wbr><wbr>insert <wbr>into <wbr><wbr>values(v);  <wbr>
  8.      -> <wbr><wbr><wbr><wbr><wbr><wbr>set v=v+1;  <wbr><wbr>
  9.      -> until v>=5  <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  10.      -> <wbr><wbr><wbr><wbr><wbr><wbr>end repeat;  <wbr><wbr>
  11.      -> <wbr><wbr><wbr><wbr><wbr><wbr>end;  <wbr>
  12.      -> //  <wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  13. mysql DELIMITER ;  <wbr><wbr><wbr><wbr>

 <wbr>


. loop ·····end loop:

loop迴圈不需要初始條件,這點和while 迴圈相似,同時和repeat迴圈一樣不需要結束條件, leave語句的意義是離開迴圈。

  1. mysql DELIMITER //  <wbr><wbr><wbr><wbr>
  2. mysql <wbr><wbr>CREATE <wbr>PROCEDURE proc6 ()  <wbr><wbr><wbr>
  3.      -> <wbr><wbr><wbr><wbr><wbr><wbr>begin <wbr>
  4.      -> <wbr><wbr><wbr><wbr><wbr><wbr>declare <wbr><wbr>int;  <wbr>
  5.      -> <wbr><wbr><wbr><wbr><wbr><wbr>set v=0;  <wbr><wbr>
  6.      -> LOOP_LABLE:loop  <wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  7.      -> <wbr><wbr><wbr><wbr><wbr><wbr>insert <wbr>into <wbr><wbr>values(v);  <wbr>
  8.      -> <wbr><wbr><wbr><wbr><wbr><wbr>set v=v+1;  <wbr><wbr>
  9.      -> if >=5 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>then <wbr>
  10.      -> leave LOOP_LABLE;  <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  11.      -> <wbr><wbr><wbr><wbr><wbr><wbr>end if;  <wbr><wbr>
  12.      -> <wbr><wbr><wbr><wbr><wbr><wbr>end loop;  <wbr><wbr>
  13.      -> <wbr><wbr><wbr><wbr><wbr><wbr>end;  <wbr>
  14.      -> //  <wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  15. mysql DELIMITER ;  <wbr><wbr><wbr><wbr>

 <wbr>

 <wbr>

. LABLES 標號:

標號可以用在begin repeat while 或者loop 語句前,語句標號只能在合法的語句前面使用。可以跳出迴圈,使執行指令達到複合語句的最後一步。

 <wbr>

(4). ITERATE迭代

. ITERATE:

通過引用複合語句的標號,來從新開始複合語句

  1. mysql DELIMITER //  <wbr><wbr><wbr><wbr>
  2. mysql <wbr><wbr>CREATE <wbr>PROCEDURE proc10 ()  <wbr><wbr><wbr>
  3.      -> <wbr><wbr><wbr><wbr><wbr><wbr>begin <wbr>
  4.      -> <wbr><wbr><wbr><wbr><wbr><wbr>declare <wbr><wbr>int;  <wbr>
  5.      -> <wbr><wbr><wbr><wbr><wbr><wbr>set v=0;  <wbr><wbr>
  6.      -> LOOP_LABLE:loop  <wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  7.      -> if v=3 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>then   <wbr><wbr>
  8.      -> <wbr><wbr><wbr><wbr><wbr><wbr>set v=v+1;  <wbr><wbr>
  9.      -> ITERATE LOOP_LABLE;  <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  10.      -> <wbr><wbr><wbr><wbr><wbr><wbr>end if;  <wbr><wbr>
  11.      -> <wbr><wbr><wbr><wbr><wbr><wbr>insert <wbr>into <wbr><wbr>values(v);  <wbr>
  12.      -> <wbr><wbr><wbr><wbr><wbr><wbr>set v=v+1;  <wbr><wbr>
  13.      -> if v>=5 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>then <wbr>
  14.      -> leave LOOP_LABLE;  <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  15.      -> <wbr><wbr><wbr><wbr><wbr><wbr>end if;  <wbr><wbr>
  16.      -> <wbr><wbr><wbr><wbr><wbr><wbr>end loop;  <wbr><wbr>
  17.      -> <wbr><wbr><wbr><wbr><wbr><wbr>end;  <wbr>
  18.      -> //  <wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  19. mysql DELIMITER <wbr><wbr><wbr><wbr>

 <wbr>

 <wbr>

9.     <wbr><wbr><wbr><wbr><wbr> MySQL儲存過程的基本函式

 <wbr>

(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 ]) //
strposition開始,length個字元
,
注:mysql中處理字串時,預設第一個字元下標為1,即引數position必須大於等於1
 <wbr>

 <wbr>

  1. mysql> <wbr>select <wbr>substring('abcd',0,2);  <wbr>
  2. +-----------------------+  <wbr>
  3. <wbr>substring('abcd',0,2) |  <wbr><wbr>
  4. +-----------------------+  <wbr>
  5.                       |  <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  6. +-----------------------+  <wbr>
  7. row <wbr><wbr>in <wbr>set (0.00 sec)  <wbr><wbr><wbr>
  8.  <wbr>
  9. mysql> <wbr>select <wbr>substring('abcd',1,2);  <wbr>
  10. +-----------------------+  <wbr>
  11. <wbr>substring('abcd',1,2) |  <wbr><wbr>
  12. +-----------------------+  <wbr>
  13.     ab                |  <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  14. +-----------------------+  <wbr>
  15. row <wbr><wbr>in <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)
預設變為整形值

  1. mysql> <wbr>select round(1.23);  <wbr><wbr>
  2. +-------------+  <wbr>
  3. round(1.23) |  <wbr><wbr><wbr>
  4. +-------------+  <wbr>
  5.           |  <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  6. +-------------+  <wbr>
  7. row <wbr><wbr>in <wbr>set (0.00 sec)  <wbr><wbr><wbr>
  8.  <wbr>
  9. mysql> <wbr>select round(1.56);  <wbr><wbr>
  10. +-------------+  <wbr>
  11. round(1.56) |  <wbr><wbr><wbr>
  12. +-------------+  <wbr>
  13.           |  <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  14. +-------------+  <wbr>
  15. row <wbr><wbr>in <wbr>set (0.00 sec) <wbr><wbr><wbr>



(2)
可以設定小數位數,返回浮點型資料

  1. mysql> <wbr>select round(1.567,2);  <wbr><wbr>
  2. +----------------+  <wbr>
  3. round(1.567,2) |  <wbr><wbr><wbr>
  4. +----------------+  <wbr>
  5.           1.57 |  <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>
  6. +----------------+  <wbr>
  7. row <wbr><wbr>in <wbr>set (0.00 sec) <wbr><wbr><wbr>

SIGN (number2 ) //

 <wbr>

(3).日期時間類

ADDTIME (date2 ,time_interval ) //time_interval加到date2
CONVERT_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) //
開平方

相關文章