mysql數(shù)據(jù)庫(kù)存儲(chǔ)過程之游標(biāo)(光標(biāo)cursor)詳解
游標(biāo)是用來存儲(chǔ)查詢結(jié)果集的數(shù)據(jù)類型,在存儲(chǔ)過程和函數(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,來查詢用戶表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ǔ)過程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可以用來定義在流程控制結(jié)構(gòu)執(zhí)行過程中遇到問題時(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ǔ)過程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ǔ)過程-游標(biāo) CURSOR FOR1、游標(biāo)
游標(biāo)是一個(gè)存儲(chǔ)在MySQL服務(wù)器上的數(shù)據(jù)庫(kù)查詢,它不是一條select語(yǔ)句,而是被該語(yǔ)句所檢索出來的結(jié)果集。
2、定義游標(biāo)
這個(gè)過程并沒有檢索到數(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來取出數(shù)據(jù)
fetch t_cursor in variable;6、關(guān)閉游標(biāo)
close t_cursor;過程:定義游標(biāo)(使用游標(biāo)前必須先定義游標(biāo))—》打開游標(biāo)—》關(guān)閉游標(biāo)
總結(jié)以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 如何實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)的備份與恢復(fù)2. MYSQL數(shù)據(jù)庫(kù)存文本轉(zhuǎn)存數(shù)據(jù)庫(kù)問題3. MySQL存儲(chǔ)過程例子(包含事務(wù)、參數(shù)、嵌套調(diào)用、游標(biāo)循環(huán)等)4. MySQL數(shù)據(jù)庫(kù)鎖機(jī)制原理解析5. MySQL數(shù)據(jù)庫(kù)node使用詳解6. 導(dǎo)出錯(cuò)誤編碼的mysql數(shù)據(jù)庫(kù)7. mysql數(shù)據(jù)庫(kù)中最常用的時(shí)間轉(zhuǎn)換函數(shù)的用法8. Mysql存儲(chǔ)過程如何實(shí)現(xiàn)歷史數(shù)據(jù)遷移9. 10個(gè)教程教你輕松備份MySQL數(shù)據(jù)庫(kù)10. mysql存儲(chǔ)過程原理與用法詳解
