MySQL預存程序

來源:互聯網
上載者:User

標籤:mysql   預存程序   事務   分頁   輸出參數   

MySQL預存程序和MSSQL的區別

1. MSSQL的關鍵字procedure可以縮寫為proc,而MySQL不能;

2. MySQL參數要申明是in、out還是inout,而MSSQL不用;

3. MySQL用CALL調用預存程序,MSSQL用EXEC;

4. MySQL有輸出參數時要用變數@賦值並查詢;

關鍵字參數、分頁執行個體1:
mysql> create procedure cs_ro_getquestionlist    -> (    -> in b int,-- 起始位置    -> in l int,-- 條數    -> in kw nvarchar(100)-- 關鍵字    -> )    -> begin    -> select id,title from w_question where locate(kw,title)>0 limit b,l;    -> end;mysql> call cs_ro_getquestionlist(0,10,‘中國‘);


含有輸入、輸出參數執行個體2:
mysql> create procedure cs_ro_getquestionlist    -> (    -> in b int,-- 起始位置    -> in l int,-- 條數    -> in kw nvarchar(100),-- 關鍵字    -> out t int-- 返回總數    -> )    -> begin    -> select count(1) into t from w_question where locate(kw,title)>0;    -> select id,title from w_question where locate(kw,title)>0 limit b,l;    -> end;mysql> call cs_ro_getquestionlist(0,10,‘中國‘,@a);mysql> select @a;


交易處理執行個體3:
mysql> create procedure cs_px_addclass    -> (    -> in nid int,    -> in nname nvarchar(20),    -> in nQuestionNum int,    -> in npx int,    -> in npycode nvarchar(20)    -> )    -> begin    -> DECLARE t_error INTEGER DEFAULT 0;    -> DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1;    -> START TRANSACTION;-- 事務開始    -> insert into w_questionclass(id,name,questionnum,px,pycode)    -> VALUES (nid,nname,nquestionnum,npx,npycode);    -> IF t_error = 1 THEN-- 如果失敗則復原    -> ROLLBACK;    -> ELSE    -> COMMIT;-- 執行成功,事務結束    -> END IF;    -> select t_error;-- 將事務的執行狀態返回給被調者    -> end;mysql> call cs_px_addclass(1,‘測試‘,1,1,‘cs‘);

+--------+

| t_error|

+--------+

|      1 |

+--------+


本文出自 “sukun” 部落格,請務必保留此出處http://sukunwu.blog.51cto.com/10453116/1688340

MySQL預存程序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.