MySQL:procedure, function, cursor,handler

來源:互聯網
上載者:User

標籤:doc   www   character   class   nbsp   注意   相關   code   odi   

Procedure & Function

Procedure 文法:

CREATE    [DEFINER = { user | CURRENT_USER }]    PROCEDURE sp_name ([proc_parameter[,...]])    [characteristic ...] routine_bodyproc_parameter:    [ IN | OUT | INOUT ] param_name typetype:    Any valid MySQL data typecharacteristic:    COMMENT ‘string‘  | LANGUAGE SQL  | [NOT] DETERMINISTIC  | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }  | SQL SECURITY { DEFINER | INVOKER }begin    Valid SQL routine statementend;

 

Function 文法:

CREATE    [DEFINER = { user | CURRENT_USER }]    FUNCTION sp_name ([func_parameter[,...]])    RETURNS type    [characteristic ...] routine_bodyfunc_parameter:    param_name typetype:    Any valid MySQL data typecharacteristic:    COMMENT ‘string‘  | LANGUAGE SQL  | [NOT] DETERMINISTIC  | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }  | SQL SECURITY { DEFINER | INVOKER }begin    Valid SQL routine statementend;

官網關於 procedure, function相關文檔:

  FAQ:http://dev.mysql.com/doc/refman/5.6/en/faqs-stored-procs.html

  文法說明:http://dev.mysql.com/doc/refman/5.6/en/create-procedure.html

 

Cursor

Cursor官方文檔:http://dev.mysql.com/doc/refman/5.6/en/cursors.html

在遍曆時,mysql中的3種迴圈方式(loop, while, repeat)都可以使用。官方文檔中給了 loop 方式的deamo。

在使用cursor時要注意:

1)declare cursor之前不能有任何的除了declare以外的操作,也就是之前只能有變數聲明。

2)declar cursor 之後不能有任何變數的聲明,可以聲明異常處理 handler。

3)cursor 只能在procedure, function中。

4)fetch into var1, var2。這裡的var名不能與 declare cursor時select 中的列名一樣。如果一樣會fetch 到NULL。例如下面deamon中的 metric ==> m 。

其它的deamon:http://www.mysqltutorial.org/mysql-cursor/

 

Handler

在什麼樣的條件下,做什麼樣的處理。例如當發生異常時,該怎麼做。

相關文檔:http://dev.mysql.com/doc/refman/5.6/en/declare-handler.html

在下面的deamon中就有declare continue handler NOT FOUND 、declare continue handler SQLSTATE 等。

 

Deamo

 

Debugger Tool

http://mydebugger.com/quick_start.php

 

 

上面 的兩個procedure,在使用debugger調試時,只需要在main中寫直接調用 就可以了。

 

MySQL:procedure, function, cursor,handler

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.