欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mysql數(shù)據(jù)庫存儲過程之游標(biāo)(光標(biāo)cursor)詳解

 更新時(shí)間:2023年07月06日 09:40:02   作者:晚安果汁  
這篇文章主要介紹了mysql數(shù)據(jù)庫存儲過程之游標(biāo)(光標(biāo)cursor)詳解,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

mysql存儲過程之游標(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)建一個(gè)存儲過程
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;
#查詢年齡小于30
call p11(30);

三、條件處理程序

條件處理程序handler可以用來定義在流程控制結(jié)構(gòu)執(zhí)行過程中遇到問題時(shí)相應(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)碼,如02000
  • SQLwarning:所有以01開頭的SQLstate代碼的簡寫
  • not found:所有以02開頭的SQLSTATE代碼的簡寫
  • SQLexception:所有沒有被SQLwarning或not found捕獲的SQLstate代碼的簡寫

2、解決報(bào)錯(cuò)

#創(chuàng)建一個(gè)存儲過程
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;
#查詢年齡小于30
call p11(30);

mysql存儲過程-游標(biāo) CURSOR FOR

1、游標(biāo)

游標(biāo)是一個(gè)存儲在MySQL服務(wù)器上的數(shù)據(jù)庫查詢,它不是一條select語句,而是被該語句所檢索出來的結(jié)果集。

2、定義游標(biāo)

這個(gè)過程并沒有檢索到數(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é)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論