mysql數(shù)據(jù)庫(kù)存儲(chǔ)過(guò)程之游標(biāo)(光標(biāo)cursor)詳解
游標(biāo)是用來(lái)存儲(chǔ)查詢結(jié)果集的數(shù)據(jù)類型,在存儲(chǔ)過(guò)程和函數(shù)中可以使用游標(biāo)對(duì)結(jié)果集進(jìn)行循環(huán)的處理。
游標(biāo)的使用包括游標(biāo)的聲明、open、fetch和close。
一、語(yǔ)法#聲明游標(biāo)declare 游標(biāo)名稱 cursor for 查詢語(yǔ)句;#開啟游標(biāo)open 游標(biāo)名稱;#獲取游標(biāo)記錄fetch 游標(biāo)名稱 into 變量[,變量];#關(guān)閉游標(biāo)close 游標(biāo)名稱;二、案例根據(jù)傳入的參數(shù)uage,來(lái)查詢用戶表tb_user中,所有的用戶年齡小于等于uage的用戶姓名name和專業(yè)profession,并將用戶的姓名和專業(yè)插入到所創(chuàng)建的一張新表id,name,profession中。
邏輯
#A.聲明游標(biāo),存儲(chǔ)查詢結(jié)果集
#B.創(chuàng)建表結(jié)構(gòu)
#C.開啟游標(biāo)
#D.獲取游標(biāo)記錄
#E.插入數(shù)據(jù)到新表中
#F.關(guān)閉游標(biāo)
#創(chuàng)建一個(gè)存儲(chǔ)過(guò)程create procedure p11(in uage int)begin? declare uname varchar(100);#聲明變量? declary upro varchar(100);#聲明變量#聲明游標(biāo)記錄符合條件的結(jié)果集? declare u_cursor cursor for select name,profession from tb_user where age <= uage;? drop table if exists tb_user_pro; ?#tb_user_pro表如果存在,就刪除。? create table if exists tb_user_pro( ?#if exists代表表存在就刪除了再創(chuàng)建表? id int primary key auto_increment,? name varchar(100),? profession varchar(100)? );? open u_cursor;#開啟游標(biāo)#while循環(huán)獲取游標(biāo)當(dāng)中的數(shù)據(jù)? while true do? fetch u_cursor into uname,upro;#獲取游標(biāo)中的記錄? insert into tb_user_pro values(null,uname,upro);#將獲取到的數(shù)據(jù)插入表結(jié)構(gòu)中? end while;? close u_cursor;#關(guān)閉游標(biāo)end;#查詢年齡小于30call p11(30);三、條件處理程序條件處理程序handler可以用來(lái)定義在流程控制結(jié)構(gòu)執(zhí)行過(guò)程中遇到問(wèn)題時(shí)相應(yīng)的處理步驟。
1、語(yǔ)法
declare handler_action handler for condition_value [,condition_value]... statement;handler_action
continue:繼續(xù)執(zhí)行當(dāng)前程序exit:終止執(zhí)行當(dāng)前程序condition_value
SQLSTATE sqlstate_value:狀態(tài)碼,如02000SQLwarning:所有以01開頭的SQLstate代碼的簡(jiǎn)寫not found:所有以02開頭的SQLSTATE代碼的簡(jiǎn)寫SQLexception:所有沒有被SQLwarning或not found捕獲的SQLstate代碼的簡(jiǎn)寫2、解決報(bào)錯(cuò)
#創(chuàng)建一個(gè)存儲(chǔ)過(guò)程create procedure p11(in uage int)begin? declare uname varchar(100);#聲明變量? declary upro varchar(100);#聲明變量#聲明游標(biāo)記錄符合條件的結(jié)果集? declare u_cursor cursor for select name,profession from tb_user where age <= uage;#聲明一個(gè)條件處理程序,當(dāng)滿足SQL狀態(tài)碼為02000的時(shí)候,觸發(fā)退出操作,退出的時(shí)候?qū)⒂螛?biāo)關(guān)閉? declare exit handler for SQLSTATE '02000' close u_cursorl;#聲明一個(gè)條件處理程序,當(dāng)滿足SQL狀態(tài)碼為02000的時(shí)候,觸發(fā)退出操作,退出的時(shí)候?qū)⒂螛?biāo)關(guān)閉? declare exit handler for not found close u_cursorl;drop table if exists tb_user_pro; ?#tb_user_pro表如果存在,就刪除。? create table if exists tb_user_pro( ?#if exists代表表存在就刪除了再創(chuàng)建表? id int primary key auto_increment,? name varchar(100),? profession varchar(100)? );? open u_cursor;#開啟游標(biāo)#while循環(huán)獲取游標(biāo)當(dāng)中的數(shù)據(jù)? while true do? fetch u_cursor into uname,upro;#獲取游標(biāo)中的記錄? insert into tb_user_pro values(null,uname,upro);#將獲取到的數(shù)據(jù)插入表結(jié)構(gòu)中? end while;? close u_cursor;#關(guān)閉游標(biāo)end;#查詢年齡小于30call p11(30);mysql存儲(chǔ)過(guò)程-游標(biāo) CURSOR FOR1、游標(biāo)
游標(biāo)是一個(gè)存儲(chǔ)在MySQL服務(wù)器上的數(shù)據(jù)庫(kù)查詢,它不是一條select語(yǔ)句,而是被該語(yǔ)句所檢索出來(lái)的結(jié)果集。
2、定義游標(biāo)
這個(gè)過(guò)程并沒有檢索到數(shù)據(jù),只是定義要使用的select語(yǔ)句
DECLARE t_cursor CURSOR FOR SELECT t.id FROM t_dept t;3、如果沒有數(shù)據(jù)返回或者select出現(xiàn)異常,程序繼續(xù),并將變量done設(shè)為true
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=true;4、打開游標(biāo)
open t_cursor;5、使用游標(biāo)
使用fetch來(lái)取出數(shù)據(jù)
fetch t_cursor in variable;6、關(guān)閉游標(biāo)
close t_cursor;過(guò)程:定義游標(biāo)(使用游標(biāo)前必須先定義游標(biāo))—》打開游標(biāo)—》關(guān)閉游標(biāo)
總結(jié)以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 導(dǎo)出錯(cuò)誤編碼的mysql數(shù)據(jù)庫(kù)2. Mysql入門系列:安排預(yù)防性的維護(hù)MYSQL數(shù)據(jù)庫(kù)服務(wù)器3. 如何實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)的備份與恢復(fù)4. Mysql數(shù)據(jù)庫(kù)設(shè)計(jì)三范式實(shí)例解析5. django將圖片保存到mysql數(shù)據(jù)庫(kù)并展示在前端頁(yè)面的實(shí)現(xiàn)6. MySQL數(shù)據(jù)庫(kù)基礎(chǔ)篇之入門基礎(chǔ)命令小結(jié)7. 如何修改Linux服務(wù)器中的MySQL數(shù)據(jù)庫(kù)密碼8. MySQL數(shù)據(jù)庫(kù)的索引原理與慢SQL優(yōu)化的5大原則9. docker中使用mysql數(shù)據(jù)庫(kù)實(shí)現(xiàn)局域網(wǎng)訪問(wèn)10. MySQL數(shù)據(jù)庫(kù)索引原理及優(yōu)化策略
