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

2022-09-29 22:36:22 字數 3899 閱讀 2097

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 fetchsewww.cppcns.comqok boolean; ## define the flag for loop judgement

declare _seqname varchar(50); ## define the varient for store the data

dwww.cppcns.comeclare _value bigint(20);

declare f程式設計客棧etchseqcursor 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

le**e 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

le**e 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自動**。

本文標題: mysql的游標的定義使用及關閉深入分析

本文位址: /shujuku/mysql/90742.html

mysql 游標的使用

可以用在儲存過程的sql語句主要有以下型別 1 無返回結果語句,如 insert,update,drop,delete等 2 select語句返回單行變數並可傳給本地變數 select into 3 返回多行結果集的select語句,並可使用游標迴圈處理 注意,儲存過程返回的多行結果集,可以被客戶端...

mysql游標的使用

這是乙個游標的使用例子.但是其中有幾點需要注意,就是為什麼要加入declare continue handler for sqlstate 02000 set tmpname null 這樣的一句話.如果不加的話將直接報錯.no data zero rows fetched,selected,or ...

MySQL游標的使用

以下的文章主要介紹的是mysql游標的使用筆記,其可以用在儲存過程的sql語句,其主要型別主要有以下幾種,以下就是對其詳細介紹,相信如果你掌握了這項技術,會在以後的學習或是工作中帶來很大的幫助。1 無返回結果語句,如 insert,update,drop,delete等 2 select語句返回單行...