mysql數(shù)據(jù)庫存儲過程之游標(biāo)(光標(biāo)cursor)詳解
游標(biāo)是用來存儲查詢結(jié)果集的數(shù)據(jù)類型,在存儲過程和函數(shù)中可以使用游標(biāo)對結(jié)果集進(jìn)行循環(huán)的處理。
游標(biāo)的使用包括游標(biāo)的聲明、open、fetch和close。
一、語法#聲明游標(biāo)declare 游標(biāo)名稱 cursor for 查詢語句;#開啟游標(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),存儲查詢結(jié)果集
#B.創(chuàng)建表結(jié)構(gòu)
#C.開啟游標(biāo)
#D.獲取游標(biāo)記錄
#E.插入數(shù)據(jù)到新表中
#F.關(guān)閉游標(biāo)
#創(chuàng)建一個存儲過程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í)行過程中遇到問題時相應(yīng)的處理步驟。
1、語法
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代碼的簡寫not found:所有以02開頭的SQLSTATE代碼的簡寫SQLexception:所有沒有被SQLwarning或not found捕獲的SQLstate代碼的簡寫2、解決報錯
#創(chuàng)建一個存儲過程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;#聲明一個條件處理程序,當(dāng)滿足SQL狀態(tài)碼為02000的時候,觸發(fā)退出操作,退出的時候?qū)⒂螛?biāo)關(guān)閉? declare exit handler for SQLSTATE '02000' close u_cursorl;#聲明一個條件處理程序,當(dāng)滿足SQL狀態(tài)碼為02000的時候,觸發(fā)退出操作,退出的時候?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存儲過程-游標(biāo) CURSOR FOR1、游標(biāo)
游標(biāo)是一個存儲在MySQL服務(wù)器上的數(shù)據(jù)庫查詢,它不是一條select語句,而是被該語句所檢索出來的結(jié)果集。
2、定義游標(biāo)
這個過程并沒有檢索到數(shù)據(jù),只是定義要使用的select語句
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é)以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. mysql數(shù)據(jù)庫中最常用的時間轉(zhuǎn)換函數(shù)的用法2. Eclipse與MySQL數(shù)據(jù)庫的連接教程(已實(shí)操)3. MySQL存儲過程in、out和inout參數(shù)示例和總結(jié)4. MySQL存儲過程的傳參和流程控制示例講解5. C++連接并使用MySQL數(shù)據(jù)庫6. MySQL數(shù)據(jù)庫的索引原理與慢SQL優(yōu)化的5大原則7. MySQL存儲過程的查詢命令介紹8. Mysql存儲過程如何實(shí)現(xiàn)歷史數(shù)據(jù)遷移9. Mysql數(shù)據(jù)庫定時備份腳本分享10. golang實(shí)現(xiàn)mysql數(shù)據(jù)庫事務(wù)的提交與回滾
