MYSQL儲存過程實現遍歷結果集更新

2021-09-24 20:01:31 字數 1061 閱讀 3360

drop procedure update_product;

delimiter  //

-- 建立儲存過程

create procedure update_product()

begin

-- 定義變數

declare s int default 0;

declare tui varchar(8);

declare product_id int;

declare countproduct int;

-- 定義游標,並將sql結果集賦值到游標中

declare report cursor for select t.tui from tui_record t  where is_use=0  order by tui asc  limit 0,167 ;

-- 宣告當游標遍歷完後將標誌變數置成某個值

declare continue handler for not found set s=1;

-- 開啟游標

open report;

-- 將游標中的值賦值給變數,注意:變數名不要和返回的列名同名,變數順序要和sql結果列的順序一致

fetch report into tui;

-- 當s不等於1,也就是未遍歷完時,會一直迴圈

while s<>1 do

-- 執行業務邏輯

select id into product_id from product_info  where substr(ctei,1,2)<>18 and `status`<>3 limit 0,1;

update product_info set ctei=tui where id=product_id;

update tui_record t set is_use=2 where t.tui=tui;

fetch report into tui;

-- 當s等於1時表明遍歷以完成,退出迴圈

end while;

-- 關閉游標

close report;

end;

//delimiter ;

mysql用儲存過程迴圈遍歷操作結果集

最近有個需求,要批量建立一批資料,但要以另一張表篩選出的結果集作為條件,邏輯很簡單,只是需要用到儲存過程,之前都沒用過,所以寫篇文章記錄下,下面的 可以作為示例。建立儲存過程之前需判斷該儲存過程是否已存在,若存在則刪除 drop procedure if exists init template 將...

ibatis中mysql儲存過程返回結果集

mysql儲存過程可以直接返回結果集,同時可以有out引數 例如 儲存過程 dao 的實現 定義乙個傳參的map params hashmapparams new hashmap 把需要的引數放到map中 params.put id account.getid params.put ret null...

mysql儲存登入 MYSQL儲存過程實現使用者登入

create definer root function uc session login re son json,srvjson json returnsjson language sqlnotdeterministiccontainssql sql security definer commen...