国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

Mysql的游標的定義使用及關閉深入分析

2024-07-24 13:01:27
字體:
來源:轉載
供稿:網友
Mysql從5.0開始支持存儲過程和trigger,給我們喜歡用mysql的朋友們更喜歡mysql的理由了,語法上和PL/SQL有差別,不過搞過編程的人都知道,語法不是問題,關鍵是思想,大致了解語法后,就從變量定義,循環,判斷,游標,異常處理這個幾個方面詳細學習了。關于游標的用法Mysql現在提供的還很特別,雖然使用起來沒有PL/SQL那么順手,不過使用上大致上還是一樣,

定義游標
declare fetchSeqCursor cursor for select seqname, value from sys_sequence;

使用游標
open fetchSeqCursor;
fetch數據
fetch cursor into _seqname, _value;

關閉游標
close fetchSeqCursor;
不過這都是針對cursor的操作而已,和PL/SQL沒有什么區別吧,不過光是了解到這個是根本不足以寫出Mysql的fetch過程的,還要了解其他的更深入的知識,我們才能真正的寫出好的游標使用的procedure
首先fetch離不開循環語句,那么先了解一下循環吧。
我一般使用Loop和while覺得比較清楚,而且代碼簡單。

這里使用Loop為例

復制代碼 代碼如下:


fetchSeqLoop:Loop
fetch cursor into _seqname, _value;
end Loop;


現在是死循環,還沒有退出的條件,那么在這里和oracle有區別,Oracle的PL/SQL的指針有個隱性變量%notfound,Mysql是通過一個Error handler的聲明來進行判斷的,
declare continue handler for Not found (do some action);
在Mysql里當游標遍歷溢出時,會出現一個預定義的NOT FOUND的Error,我們處理這個Error并定義一個continue的handler就可以叻,關于Mysql Error handler可以查詢Mysql手冊定義一個flag,在NOT FOUND,標示Flag,在Loop里以這個flag為結束循環的判斷就可以叻。

復制代碼 代碼如下:


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;


這就是一個完整的過程叻,那么會思考的人一般在這里都會思考,如果是這樣的話,怎樣做嵌套的游標循環叻,這里可以根據statement block的scope實現叻,Mysql里通過begin end來劃分一個statement block,在block里定義的變量范圍也在這個block里,所以關于嵌套的游標循環我們可以多加一個begin end來區分他們所對應的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;


這樣就可以輕松實現更多層次的循環了,不過相對oracle的PL/SQL來說,Mysql現在還不支持動態游標的定義,所以很強大的動態拼出SQL的在游標里還不能做到,不過這完全不影響我對Mysql的喜愛程度,她就想那羞澀的荷花一樣,雖然沒有燦爛的色彩,但那簡約的色調,清新而不染一絲鉛塵的高雅,一樣吸引著無數的mysql迷么,正如接天蓮葉無窮碧,映日荷花別樣紅。

付:Mysql也有類似Oracle里的execute immediate的動態SQL的功能,通過這個功能可有多少彌補一些動態游標的缺憾叻
set @sqlStr='select * from table where condition1 = ?';
prepare s1 for @sqlStr;
execute s1 using @condition1; 如果有多個參數用逗號分隔
deallocate prepare s1; 手工釋放,或者是connection關閉時,server自動回收。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙陵县| 墨江| 牟定县| 三河市| 张家界市| 韩城市| 文山县| 马鞍山市| 中西区| 庆云县| 阜宁县| 溧阳市| 大新县| 福州市| 威宁| 渭源县| 鄂托克前旗| 阳春市| 宁乡县| 伊春市| 三江| 巴楚县| 赞皇县| 新密市| 波密县| 浑源县| 庄河市| 焦作市| 安乡县| 长寿区| 边坝县| 曲松县| 双牌县| 吉首市| 竹山县| 琼中| 颍上县| 海丰县| 萨迦县| 吐鲁番市| 梧州市|