最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Mysql 游標(biāo)的用法及其作用

 更新時間:2020年09月07日 15:38:07   作者:Cyylog  
這篇文章主要介紹了Mysql 游標(biāo)的相關(guān)資料,幫助大家更好的理解和使用MySQL數(shù)據(jù)庫,感興趣的朋友可以了解下

[mysql游標(biāo)的用法及作用]

例子:

當(dāng)前有三張表A、B、C其中A和B是一對多關(guān)系,B和C是一對多關(guān)系,現(xiàn)在需要將B中A表的主鍵存到C中;
常規(guī)思路就是將B中查詢出來然后通過一個update語句來更新C表就可以了,但是B表中有2000多條數(shù)據(jù),
難道要執(zhí)行2000多次?顯然是不現(xiàn)實的;最終找到寫一個存儲過程然后通過循環(huán)來更新C表,
然而存儲過程中的寫法用的就是游標(biāo)的形式。

【簡介】

游標(biāo)實際上是一種能從包括多條數(shù)據(jù)記錄的結(jié)果集中每次提取一條記錄的機制。

​ 游標(biāo)充當(dāng)指針的作用。

​ 盡管游標(biāo)能遍歷結(jié)果中的所有行,但他一次只指向一行。

​ 游標(biāo)的作用就是用于對查詢數(shù)據(jù)庫所返回的記錄進行遍歷,以便進行相應(yīng)的操作。

【用法】

 一、聲明一個游標(biāo): declare 游標(biāo)名稱 CURSOR for table;(這里的table可以是你查詢出來的任意集合)
​ 二、打開定義的游標(biāo):open 游標(biāo)名稱;
​ 三、獲得下一行數(shù)據(jù):FETCH 游標(biāo)名稱 into testrangeid,versionid;
​ 四、需要執(zhí)行的語句(增刪改查):這里視具體情況而定
​ 五、釋放游標(biāo):CLOSE 游標(biāo)名稱;

注:mysql存儲過程每一句后面必須用;結(jié)尾,使用的臨時字段需要在定義游標(biāo)之前進行聲明。

【實例】

- 
BEGIN 
 
--定義變量 
declare testrangeid BIGINT; 
declare versionid BIGINT; 
declare done int; 
--創(chuàng)建游標(biāo),并存儲數(shù)據(jù) 
declare cur_test CURSOR for 
 select id as testrangeid,version_id as versionid from tp_testrange; 
--游標(biāo)中的內(nèi)容執(zhí)行完后將done設(shè)置為1 
 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1; 
--打開游標(biāo) 
open cur_test; 
--執(zhí)行循環(huán) 
 posLoop:LOOP 
--判斷是否結(jié)束循環(huán) 
  IF done=1 THEN 
  LEAVE posLoop; 
 END IF; 
--取游標(biāo)中的值 
 FETCH cur_test into testrangeid,versionid; 
--執(zhí)行更新操作 
 update tp_data_execute set version_id=versionid where testrange_id = testrangeid; 
 END LOOP posLoop; 
--釋放游標(biāo) 
CLOSE cur_test; 
 
END 
-

例子2:

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

--在windows系統(tǒng)中寫存儲過程時,如果需要使用declare聲明變量,需要添加這個關(guān)鍵字,否則會報錯。 
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é)束時的返回值 
 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ù)時,將count值進行累加操作,這里可以做任意你想做的操作, 
 set total = total + c; 
 --結(jié)束游標(biāo)循環(huán) 
 end loop; 
 --關(guān)閉游標(biāo) 
 close cur; 
 
 --輸出結(jié)果 
 select total; 
END; 
--調(diào)用存儲過程 
call StatisticStore();

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

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

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

declare continue HANDLER for not found set done = true;

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

--判斷游標(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)有三種使用方式:
第一種就是上面的實現(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中,每個begin end 塊都是一個獨立的scope區(qū)域,由于MySql中同一個error的事件只能定義一次,如果多定義的話在編譯時會提示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 then 
    leave 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();

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

動態(tài)SQL

Mysql 支持動態(tài)SQL的功能

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

以上就是詳解Mysql 游標(biāo)的詳細(xì)內(nèi)容,更多關(guān)于Mysql 游標(biāo)的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • mySQL之關(guān)鍵字的執(zhí)行優(yōu)先級講解

    mySQL之關(guān)鍵字的執(zhí)行優(yōu)先級講解

    這篇文章主要介紹了mySQL之關(guān)鍵字的執(zhí)行優(yōu)先級講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • 關(guān)于pt-archiver和自增主鍵的那些事

    關(guān)于pt-archiver和自增主鍵的那些事

    mysql是我們經(jīng)常會用到的一個數(shù)據(jù)庫,mysql數(shù)據(jù)庫中有一個主鍵生成規(guī)則,就是自增,這篇文章主要給大家介紹了關(guān)于pt-archiver和自增主鍵的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • 查找MySQL中查詢慢的SQL語句方法

    查找MySQL中查詢慢的SQL語句方法

    這篇文章主要介紹了查找MySQL中查詢慢的SQL語句方法,需要的朋友可以參考下
    2017-05-05
  • 關(guān)于case when語句的報錯問題詳解

    關(guān)于case when語句的報錯問題詳解

    SQL CASE 表達式是一種通用的條件表達式,類似于其它語言中的 if/else 語句,下面這篇文章主要給大家介紹了關(guān)于case when語句的報錯問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-08-08
  • MySQL清空數(shù)據(jù)表的方法實例與分析

    MySQL清空數(shù)據(jù)表的方法實例與分析

    這篇文章給大家詳細(xì)介紹了MySQL清空數(shù)據(jù)表的方法,文中給出了實例代碼,對大家學(xué)習(xí)使用MySQL具有一定的參考借鑒價值,有需要的可以參考借鑒,下面來一起看看吧。
    2016-10-10
  • 小白安裝登錄mysql-8.0.19-winx64的教程圖解(新手必看)

    小白安裝登錄mysql-8.0.19-winx64的教程圖解(新手必看)

    這篇文章主要介紹了安裝登錄mysql-8.0.19-winx64的教程圖解,非常適合新手學(xué)習(xí)參考,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • Mysql運行環(huán)境優(yōu)化(Linux系統(tǒng))

    Mysql運行環(huán)境優(yōu)化(Linux系統(tǒng))

    這篇文章主要介紹了Mysql運行環(huán)境優(yōu)化(Linux系統(tǒng)),本文優(yōu)化了修改Linux默認(rèn)的IO調(diào)度算法、擴大文件描述符、禁用numa特性、修改swappiness設(shè)置、優(yōu)化文件系統(tǒng)掛載參數(shù)等配置,需要的朋友可以參考下
    2015-02-02
  • MySQL主鍵批量修改的坑與解決方案

    MySQL主鍵批量修改的坑與解決方案

    在日常開發(fā)中,我們可能會遇到需要批量修改 MySQL 數(shù)據(jù)表主鍵的情況,乍一看,修改主鍵 ID 似乎是一個簡單的操作,但如果處理不當(dāng),會導(dǎo)致操作失敗甚至數(shù)據(jù)丟失,本文將詳細(xì)剖析問題成因,并總結(jié)多種安全高效的解決方案,需要的朋友可以參考下
    2024-12-12
  • log引起的mysql不能啟動的解決方法

    log引起的mysql不能啟動的解決方法

    今天服務(wù)器掛了原來服務(wù)器的硬盤進行了數(shù)據(jù)轉(zhuǎn)移 弄到mysql的時候發(fā)現(xiàn)里面log日志文件高達900MB
    2008-07-07
  • MySQL中SHOW TABLE STATUS的使用及說明

    MySQL中SHOW TABLE STATUS的使用及說明

    這篇文章主要介紹了MySQL中SHOW TABLE STATUS的使用及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10

最新評論

外汇| 新龙县| 威宁| 叶城县| 修文县| 武定县| 库尔勒市| 金寨县| 都江堰市| 霍州市| 鸡泽县| 库伦旗| 资中县| 方城县| 华坪县| 丁青县| 阳新县| 宜城市| 兴业县| 台北县| 石棉县| 福安市| 白河县| 德安县| 历史| 光山县| 丽江市| 柳江县| 汶上县| 磐安县| 宿松县| 安泽县| 中牟县| 高唐县| 东乌珠穆沁旗| 喀喇沁旗| 景泰县| 湘潭县| 方正县| 南昌市| 肇庆市|