Mysql的游標的定義使用及關(guān)閉深入分析
更新時間:2012年12月18日 17:40:57 作者:
于游標的用法Mysql現(xiàn)在提供的還很特別,雖然使用起來沒有PL/SQL那么順手,不過使用上大致上還是一樣,本文將詳細介紹一下,需要了解的朋友可以參考下
Mysql從5.0開始支持存儲過程和trigger,給我們喜歡用mysql的朋友們更喜歡mysql的理由了,語法上和PL/SQL有差別,不過搞過編程的人都知道,語法不是問題,關(guān)鍵是思想,大致了解語法后,就從變量定義,循環(huán),判斷,游標,異常處理這個幾個方面詳細學習了。關(guān)于游標的用法Mysql現(xiàn)在提供的還很特別,雖然使用起來沒有PL/SQL那么順手,不過使用上大致上還是一樣,
定義游標
declare fetchSeqCursor cursor for select seqname, value from sys_sequence;
使用游標
open fetchSeqCursor;
fetch數(shù)據(jù)
fetch cursor into _seqname, _value;
關(guān)閉游標
close fetchSeqCursor;
不過這都是針對cursor的操作而已,和PL/SQL沒有什么區(qū)別吧,不過光是了解到這個是根本不足以寫出Mysql的fetch過程的,還要了解其他的更深入的知識,我們才能真正的寫出好的游標使用的procedure
首先fetch離不開循環(huán)語句,那么先了解一下循環(huán)吧。
我一般使用Loop和while覺得比較清楚,而且代碼簡單。
這里使用Loop為例
fetchSeqLoop:Loop
fetch cursor into _seqname, _value;
end Loop;
現(xiàn)在是死循環(huán),還沒有退出的條件,那么在這里和oracle有區(qū)別,Oracle的PL/SQL的指針有個隱性變量%notfound,Mysql是通過一個Error handler的聲明來進行判斷的,
declare continue handler for Not found (do some action);
在Mysql里當游標遍歷溢出時,會出現(xiàn)一個預(yù)定義的NOT FOUND的Error,我們處理這個Error并定義一個continue的handler就可以叻,關(guān)于Mysql Error handler可以查詢Mysql手冊定義一個flag,在NOT FOUND,標示Flag,在Loop里以這個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;
這就是一個完整的過程叻,那么會思考的人一般在這里都會思考,如果是這樣的話,怎樣做嵌套的游標循環(huán)叻,這里可以根據(jù)statement block的scope實現(xiàn)叻,Mysql里通過begin end來劃分一個statement block,在block里定義的變量范圍也在這個block里,所以關(guān)于嵌套的游標循環(huán)我們可以多加一個begin end來區(qū)分他們所對應(yīng)的error handler(注意在Mysql里同一個error的handler只能定義一次,多定義的話,在compile的過程中會提示里duplicate handler defination,所以NOT FOUND的handler就只能定義一次),在一個begin end里定義這個里面游標的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;
這樣就可以輕松實現(xiàn)更多層次的循環(huán)了,不過相對oracle的PL/SQL來說,Mysql現(xiàn)在還不支持動態(tài)游標的定義,所以很強大的動態(tài)拼出SQL的在游標里還不能做到,不過這完全不影響我對Mysql的喜愛程度,她就想那羞澀的荷花一樣,雖然沒有燦爛的色彩,但那簡約的色調(diào),清新而不染一絲鉛塵的高雅,一樣吸引著無數(shù)的mysql迷么,正如接天蓮葉無窮碧,映日荷花別樣紅。
付:Mysql也有類似Oracle里的execute immediate的動態(tài)SQL的功能,通過這個功能可有多少彌補一些動態(tài)游標的缺憾叻
set @sqlStr='select * from table where condition1 = ?';
prepare s1 for @sqlStr;
execute s1 using @condition1; 如果有多個參數(shù)用逗號分隔
deallocate prepare s1; 手工釋放,或者是connection關(guān)閉時,server自動回收。
定義游標
declare fetchSeqCursor cursor for select seqname, value from sys_sequence;
使用游標
open fetchSeqCursor;
fetch數(shù)據(jù)
fetch cursor into _seqname, _value;
關(guān)閉游標
close fetchSeqCursor;
不過這都是針對cursor的操作而已,和PL/SQL沒有什么區(qū)別吧,不過光是了解到這個是根本不足以寫出Mysql的fetch過程的,還要了解其他的更深入的知識,我們才能真正的寫出好的游標使用的procedure
首先fetch離不開循環(huán)語句,那么先了解一下循環(huán)吧。
我一般使用Loop和while覺得比較清楚,而且代碼簡單。
這里使用Loop為例
復制代碼 代碼如下:
fetchSeqLoop:Loop
fetch cursor into _seqname, _value;
end Loop;
現(xiàn)在是死循環(huán),還沒有退出的條件,那么在這里和oracle有區(qū)別,Oracle的PL/SQL的指針有個隱性變量%notfound,Mysql是通過一個Error handler的聲明來進行判斷的,
declare continue handler for Not found (do some action);
在Mysql里當游標遍歷溢出時,會出現(xiàn)一個預(yù)定義的NOT FOUND的Error,我們處理這個Error并定義一個continue的handler就可以叻,關(guān)于Mysql Error handler可以查詢Mysql手冊定義一個flag,在NOT FOUND,標示Flag,在Loop里以這個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;
這就是一個完整的過程叻,那么會思考的人一般在這里都會思考,如果是這樣的話,怎樣做嵌套的游標循環(huán)叻,這里可以根據(jù)statement block的scope實現(xiàn)叻,Mysql里通過begin end來劃分一個statement block,在block里定義的變量范圍也在這個block里,所以關(guān)于嵌套的游標循環(huán)我們可以多加一個begin end來區(qū)分他們所對應(yīng)的error handler(注意在Mysql里同一個error的handler只能定義一次,多定義的話,在compile的過程中會提示里duplicate handler defination,所以NOT FOUND的handler就只能定義一次),在一個begin end里定義這個里面游標的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;
這樣就可以輕松實現(xiàn)更多層次的循環(huán)了,不過相對oracle的PL/SQL來說,Mysql現(xiàn)在還不支持動態(tài)游標的定義,所以很強大的動態(tài)拼出SQL的在游標里還不能做到,不過這完全不影響我對Mysql的喜愛程度,她就想那羞澀的荷花一樣,雖然沒有燦爛的色彩,但那簡約的色調(diào),清新而不染一絲鉛塵的高雅,一樣吸引著無數(shù)的mysql迷么,正如接天蓮葉無窮碧,映日荷花別樣紅。
付:Mysql也有類似Oracle里的execute immediate的動態(tài)SQL的功能,通過這個功能可有多少彌補一些動態(tài)游標的缺憾叻
set @sqlStr='select * from table where condition1 = ?';
prepare s1 for @sqlStr;
execute s1 using @condition1; 如果有多個參數(shù)用逗號分隔
deallocate prepare s1; 手工釋放,或者是connection關(guān)閉時,server自動回收。
相關(guān)文章
mysql跨服務(wù)查詢之FEDERATED存儲引擎的實現(xiàn)
本文主要介紹了mysql跨服務(wù)查詢之FEDERATED存儲引擎的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-01-01
MySQL中的LOCATE和POSITION函數(shù)使用方法
不常用:MySQL中的LOCATE和POSITION函數(shù)2010-02-02

