mysql儲存過程之游標使用(六)

2021-09-26 09:41:42 字數 927 閱讀 3147

需求:修改id為偶數的記錄分數

delimiter &&

create procedure test_cur_1()

begin

-- 定義開關

declare stopflag int default 0;

-- 定義資料存放的變數

declare my_id varchar(255) default '';

-- 定義游標變數

declare id_cur cursor for select id from test_score where id % 2 = 0;

-- 查詢不到游標時,設定開關為1

declare continue handler for not found set stopflag = 1;

-- 開啟游標

open id_cur;

-- 游標向前走一步,去除一條記錄放入資料變數中

fetch id_cur into my_id;

-- 游標還沒有到結尾

while(stopflag=0) do

begin

-- 更新資料

update test_score set name = concat(name,id) where id = my_id;

-- 游標向前走一步,去除一條記錄放入資料變數中

fetch id_cur into my_id;

end;

end while;

-- 關閉游標

close id_cur;

end;

&&delimiter ;

1.游標是儲存查詢結果的臨時記憶體區域

2.游標變數儲存查詢的臨時結果,實際上就是查詢結果集

儲存過程之游標

游標 指標 1 select。into 注意 into 關鍵字後的變數要求先宣告 只能出現在儲存過程和觸發器,不能單獨使用 字段 數量 型別 要和變數一致 只能針對一行 返回標量值 搜尋的結果集為多條,移動指標 步驟 建立游標 開啟游標 獲取記錄 關閉游標 語法 declare 游標名稱 curso...

mysql儲存過程之游標篇

本篇主要參考mysql手冊 游標必須在宣告處理程式之前被宣告,並且變數和條件必須在宣告游標或處理程式之前被宣告。宣告游標 declare cursor name cursor for select statement 這個語句宣告乙個游標。也可以在子程式中定義多個游標,但是乙個塊中的每乙個游標必須有...

oracle 儲存過程之游標(loop)使用

declare 宣告兩個變數 v id varchar2 50 v int number cursor yb is select a.id from t d5 punishment a where a.removed 0 begin v int 1 變數賦值 open yb 開啟游標 loop 開始...