午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

MySQL 游標(biāo)的定義與使用方式

瀏覽:14日期:2023-10-06 15:03:46
創(chuàng)建游標(biāo)

首先在MySql中創(chuàng)建一張數(shù)據(jù)表:

CREATE TABLE IF NOT EXISTS `store` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, `count` int(11) NOT NULL DEFAULT ’1’, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7; INSERT INTO `store` (`id`, `name`, `count`) VALUES(1, ’android’, 15),(2, ’iphone’, 14),(3, ’iphone’, 20),(4, ’android’, 5),(5, ’android’, 13),(6, ’iphone’, 13);

我們現(xiàn)在要用存儲(chǔ)過(guò)程做一個(gè)功能,統(tǒng)計(jì)iphone的總庫(kù)存是多少,并把總數(shù)輸出到控制臺(tái)。

--在windows系統(tǒng)中寫存儲(chǔ)過(guò)程時(shí),如果需要使用declare聲明變量,需要添加這個(gè)關(guān)鍵字,否則會(huì)報(bào)錯(cuò)。delimiter //drop procedure if exists StatisticStore;CREATE PROCEDURE StatisticStore()BEGIN --創(chuàng)建接收游標(biāo)數(shù)據(jù)的變量 declare c int; declare n varchar(20); --創(chuàng)建總數(shù)變量 declare total int default 0; --創(chuàng)建結(jié)束標(biāo)志變量 declare done int default false; --創(chuàng)建游標(biāo) declare cur cursor for select name,count from store where name = ’iphone’; --指定游標(biāo)循環(huán)結(jié)束時(shí)的返回值 declare continue HANDLER for not found set done = true; --設(shè)置初始值 set total = 0; --打開游標(biāo) open cur; --開始循環(huán)游標(biāo)里的數(shù)據(jù) read_loop:loop --根據(jù)游標(biāo)當(dāng)前指向的一條數(shù)據(jù) fetch cur into n,c; --判斷游標(biāo)的循環(huán)是否結(jié)束 if done then leave read_loop; --跳出游標(biāo)循環(huán) end if; --獲取一條數(shù)據(jù)時(shí),將count值進(jìn)行累加操作,這里可以做任意你想做的操作, set total = total + c; --結(jié)束游標(biāo)循環(huán) end loop; --關(guān)閉游標(biāo) close cur; --輸出結(jié)果 select total;END;--調(diào)用存儲(chǔ)過(guò)程call StatisticStore();

fetch是獲取游標(biāo)當(dāng)前指向的數(shù)據(jù)行,并將指針指向下一行,當(dāng)游標(biāo)已經(jīng)指向最后一行時(shí)繼續(xù)執(zhí)行會(huì)造成游標(biāo)溢出。使用loop循環(huán)游標(biāo)時(shí),他本身是不會(huì)監(jiān)控是否到最后一條數(shù)據(jù)了,像下面代碼這種寫法,就會(huì)造成死循環(huán);

read_loop:loopfetch cur into n,c;set total = total+c;end loop;

在MySql中,造成游標(biāo)溢出時(shí)會(huì)引發(fā)mysql預(yù)定義的NOT FOUND錯(cuò)誤,所以在上面使用下面的代碼指定了當(dāng)引發(fā)not found錯(cuò)誤時(shí)定義一個(gè)continue 的事件,指定這個(gè)事件發(fā)生時(shí)修改done變量的值。

declare continue HANDLER for not found set done = true;

所以在循環(huán)時(shí)加上了下面這句代碼:

--判斷游標(biāo)的循環(huán)是否結(jié)束if done then leave read_loop; --跳出游標(biāo)循環(huán)end if;

如果done的值是true,就結(jié)束循環(huán)。繼續(xù)執(zhí)行下面的代碼。

使用方式

游標(biāo)有三種使用方式:第一種就是上面的實(shí)現(xiàn),使用loop循環(huán);第二種方式如下,使用while循環(huán):

drop procedure if exists StatisticStore1;CREATE PROCEDURE StatisticStore1()BEGIN declare c int; declare n varchar(20); declare total int default 0; declare done int default false; declare cur cursor for select name,count from store where name = ’iphone’; declare continue HANDLER for not found set done = true; set total = 0; open cur; fetch cur into n,c; while(not done) do set total = total + c; fetch cur into n,c; end while; close cur; select total;END; call StatisticStore1();

第三種方式是使用repeat執(zhí)行:

drop procedure if exists StatisticStore2;CREATE PROCEDURE StatisticStore2()BEGIN declare c int; declare n varchar(20); declare total int default 0; declare done int default false; declare cur cursor for select name,count from store where name = ’iphone’; declare continue HANDLER for not found set done = true; set total = 0; open cur; repeat fetch cur into n,c; if not done then set total = total + c; end if; until done end repeat; close cur; select total;END; call StatisticStore2();游標(biāo)嵌套

在mysql中,每個(gè)begin end 塊都是一個(gè)獨(dú)立的scope區(qū)域,由于MySql中同一個(gè)error的事件只能定義一次,如果多定義的話在編譯時(shí)會(huì)提示Duplicate handler declared in the same block。

drop procedure if exists StatisticStore3;CREATE PROCEDURE StatisticStore3()BEGIN declare _n varchar(20); declare done int default false; declare cur cursor for select name from store group by name; declare continue HANDLER for not found set done = true; open cur; read_loop:loop fetch cur into _n; if done then leave read_loop; end if; begin declare c int; declare n varchar(20); declare total int default 0; declare done int default false; declare cur cursor for select name,count from store where name = ’iphone’; declare continue HANDLER for not found set done = true; set total = 0; open cur; iphone_loop:loop fetch cur into n,c; if done then leave iphone_loop; end if; set total = total + c; end loop; close cur; select _n,n,total; end; begin declare c int; declare n varchar(20); declare total int default 0; declare done int default false; declare cur cursor for select name,count from store where name = ’android’; declare continue HANDLER for not found set done = true; set total = 0; open cur; android_loop:loop fetch cur into n,c; if done thenleave android_loop; end if; set total = total + c; end loop; close cur; select _n,n,total; end; begin end; end loop; close cur;END; call StatisticStore3();

上面就是實(shí)現(xiàn)一個(gè)嵌套循環(huán),當(dāng)然這個(gè)例子比較牽強(qiáng)。湊合看看就行。。

動(dòng)態(tài)SQL

Mysql 支持動(dòng)態(tài)SQL的功能,

set @sqlStr=’select * from table where condition1 = ?’;prepare s1 for @sqlStr;--如果有多個(gè)參數(shù)用逗號(hào)分隔execute s1 using @condition1;--手工釋放,或者是 connection 關(guān)閉時(shí), server 自動(dòng)回收deallocate prepare s1;

以上就是MySQL 游標(biāo)的定義與使用方式的詳細(xì)內(nèi)容,更多關(guān)于MySQL 游標(biāo)的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: MySQL 數(shù)據(jù)庫(kù)
相關(guān)文章:
主站蜘蛛池模板: 亚洲午夜精品久久久 | 97在线免费视频 | 毛片毛片毛片 | 咪咪色影院 | 天堂自拍 | 日韩欧美高清dvd碟片 | 成人午夜网 | 日韩欧美一 | 成人在线黄色 | 天堂在线观看中文字幕 | 男人资源av| 男人的天堂免费视频 | www.五月激情 | 99精品视频在线观看免费 | 91精品一区 | 久久综合国产 | 成人国产片女人爽到高潮 | 欧美日韩亚洲激情 | 日本在线视频中文字幕 | 亚洲成人影院在线观看 | 成人欧美视频 | 国产99热| 日韩激情网站 | jizz国产精品| 黄色大片免费网站 | 日韩一级视频 | 国产v在线观看 | 欧美成人激情视频 | 国产一区不卡 | 就要操就要射 | 国产午夜在线视频 | www夜色| 日韩美女一区 | 成人免费网站在线观看 | juliaannxxx精品艳妇 | 天海翼av在线 | 日韩二区在线 | 日本中文字幕网 | 极品色综合 | 天天干,天天操 | 日批视频在线 |