《Mysql學(xué)習(xí)Mysql的游標(biāo)的定義使用及關(guān)閉深入分析》要點(diǎn):
本文介紹了Mysql學(xué)習(xí)Mysql的游標(biāo)的定義使用及關(guān)閉深入分析,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
Mysql從5.0開始支持存儲(chǔ)過(guò)程和trigger,給我們喜歡用mysql的朋友們更喜歡mysql的理由了,語(yǔ)法上和PL/SQL有差別,不過(guò)搞過(guò)編程的人都知道,語(yǔ)法不是問(wèn)題,關(guān)鍵是思想,大致了解語(yǔ)法后,就從變量定義,循環(huán),判斷,游標(biāo),異常處理這個(gè)幾個(gè)方面詳細(xì)學(xué)習(xí)了.關(guān)于游標(biāo)的用法Mysql現(xiàn)在提供的還很特別,雖然使用起來(lái)沒有PL/SQL那么順手,不過(guò)使用上大致上還是一樣,
定義游標(biāo) declare fetchSeqCursor cursor for select seqname, value from sys_sequence;
使用游標(biāo) open fetchSeqCursor;
fetch數(shù)據(jù)
fetch cursor into _seqname, _value;
關(guān)閉游標(biāo) close fetchSeqCursor;
不過(guò)這都是針對(duì)cursor的操作罷了,和PL/SQL沒有什么區(qū)別吧,不過(guò)光是了解到這個(gè)是根本不足以寫出Mysql的fetch過(guò)程的,還要了解其他的更深入的知識(shí),我們才能真正的寫出好的游標(biāo)使用的procedure
首先f(wàn)etch離不開循環(huán)語(yǔ)句,那么先了解一下循環(huán)吧.
我一般使用Loop和while覺得比較清楚,而且代碼簡(jiǎn)單.
這里使用Loop為例
代碼如下:
fetchSeqLoop:Loop
fetch cursor into _seqname, _value;
end Loop;
現(xiàn)在是死循環(huán),還沒有退出的條件,那么在這里和oracle有區(qū)別,Oracle的PL/SQL的指針有個(gè)隱性變量%notfound,Mysql是通過(guò)一個(gè)Error handler的聲明來(lái)進(jìn)行判斷的,
declare continue handler for Not found (do some action);
在Mysql里當(dāng)游標(biāo)遍歷溢出時(shí),會(huì)出現(xiàn)一個(gè)預(yù)定義的NOT FOUND的Error,我們處理這個(gè)Error并定義一個(gè)continue的handler就可以叻,關(guān)于Mysql Error handler可以查詢Mysql手冊(cè)定義一個(gè)flag,在NOT FOUND,標(biāo)示Flag,在Loop里以這個(gè)flag為結(jié)束循環(huán)的判斷就可以叻.
代碼如下:
declare fetchSeqOk boolean; ## define the flag for loop judgement
declare _seqname varchar(50); ## define the varient for store the data
declare _value bigint(20);
declare fetchSeqCursor cursor for select seqname, value from sys_sequence;## define the cursor
declare continue handler for NOT FOUND set fetchSeqOk = true; ## define the continue handler for not
found flag
set fetchSeqOk = false;
open fetchSeqCursor;
fetchSeqLoop:Loop
if fetchSeqOk then
leave fetchSeqLoop;
else
fetch cursor into _seqname, _value;
select _seqname, _value;
end if;
end Loop;
close fetchSeqCursor;
這便是一個(gè)完整的過(guò)程叻,那么會(huì)思考的人一般在這里都會(huì)思考,如果是這樣的話,怎樣做嵌套的游標(biāo)循環(huán)叻,這里可以根據(jù)statement block的scope實(shí)現(xiàn)叻,Mysql里通過(guò)begin end來(lái)劃分一個(gè)statement block,在block里定義的變量范圍也在這個(gè)block里,所以關(guān)于嵌套的游標(biāo)循環(huán)我們可以多加一個(gè)begin end來(lái)區(qū)分他們所對(duì)應(yīng)的error handler(注意在Mysql里同一個(gè)error的handler只能定義一次,多定義的話,在compile的過(guò)程中會(huì)提示里duplicate handler defination,所以NOT FOUND的handler就只能定義一次),在一個(gè)begin end里定義這個(gè)里面游標(biāo)的NOT FOUND handler,
代碼如下:
declare fetchSeqOk boolean; ## define the flag for loop judgement
declare _seqname varchar(50); ## define the varient for store the data
declare _value bigint(20);
declare fetchSeqCursor cursor for select seqname, value from sys_sequence;## define the cursor
declare continue handler for NOT FOUND set fetchSeqOk = true; ## define the continue handler for not
found flag
set fetchSeqOk = false;
open fetchSeqCursor;
fetchSeqLoop:Loop
if fetchSeqOk then
leave fetchSeqLoop;
else
fetch cursor into _seqname, _value;
begin
declare fetchSeqOk boolean default 'inner';
declare cursor2 cursor for select .... from ...;## define the cursor
declare continue handler for NOT FOUND set fetchSeqOk = true; ## define the continue handler for n
ot
set fetchSeqOk = false;
open cursor2;
fetchloop2 loop
if fetchSeqOk then
else
end if;
end loop;
close cursor2;
end;
end if;
end Loop;
close fetchSeqCursor;
這樣就可以輕松實(shí)現(xiàn)更多層次的循環(huán)了,不過(guò)相對(duì)oracle的PL/SQL來(lái)說(shuō),Mysql現(xiàn)在還不支持動(dòng)態(tài)游標(biāo)的定義,所以很強(qiáng)大的動(dòng)態(tài)拼出SQL的在游標(biāo)里還不能做到,不過(guò)這完全不影響我對(duì)Mysql的喜愛程度,她就想那羞澀的荷花一樣,雖然沒有燦爛的色彩,但那簡(jiǎn)約的色調(diào),清新而不染一絲鉛塵的高雅,一樣吸引著無(wú)數(shù)的mysql迷么,正如接天蓮葉無(wú)窮碧,映日荷花別樣紅.
付:Mysql也有類似Oracle里的execute immediate的動(dòng)態(tài)SQL的功能,通過(guò)這個(gè)功能可有多少補(bǔ)充一些動(dòng)態(tài)游標(biāo)的缺憾叻
set @sqlStr='select * from table where condition1 = ?';
prepare s1 for @sqlStr;
execute s1 using @condition1; 如果有多個(gè)參數(shù)用逗號(hào)分隔
deallocate prepare s1; 手工釋放,或者是connection關(guān)閉時(shí),server自動(dòng)回收.
維易PHP培訓(xùn)學(xué)院每天發(fā)布《Mysql學(xué)習(xí)Mysql的游標(biāo)的定義使用及關(guān)閉深入分析》等實(shí)戰(zhàn)技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養(yǎng)人才。
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/13591.html