MYSQL存储过程实现遍历结果集更新


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 ;

你可能感兴趣的:(java)