游標(biāo)cursor與loop循環(huán)的結(jié)合使用

Mysql存儲(chǔ)過(guò)程中的loop循環(huán):

drop table if exists hpbaselayout;
create table hpbaselayout(
         id int not null,
    layoutcode varchar(1000) not null,
    layoutimage varchar(1000) not null
)
;
drop table if exists pagelayout;
create table pagelayout(
         id int not null,
    layoutcode varchar(1000) not null,
    layoutimage varchar(1000) not null
)
;

DROP procedure IF EXISTS p_update_hplayoutcode;
DELIMITER //  
<!--Mysql存儲(chǔ)過(guò)程必須得帶括號(hào)(),Oracle語(yǔ)法不需要-->
create  procedure p_update_hplayoutcode()
begin  
DECLARE s_id int;
DECLARE s_layoutcode varchar(1000);
DECLARE s_layoutimage varchar(1000);
DECLARE v_sql varchar(1000);  
declare b int default 0 ;  
DECLARE t_cur CURSOR for select id,layoutcode,layoutimage from hpbaselayout;
set @num=(select count(1) from hpbaselayout);
OPEN t_cur;
<!--Mysql loop語(yǔ)法,循環(huán)開(kāi)始前需要聲明個(gè)結(jié)束標(biāo)志符以便leave-->
   loop_label:loop
    fetch  t_cur into s_id,s_layoutcode,s_layoutimage;
    update pagelayout set layoutcode=s_layoutcode,layoutimage=s_layoutimage where  id = s_id; 
    set b=b+1;
    IF b=@num THEN
      leave loop_label;
    end if;
  end loop;
  close t_cur;
  commit;
end;
// 
DELIMITER ;

call p_update_hplayoutcode()
;

Oracle存儲(chǔ)過(guò)程中的loop循環(huán):

create or replace procedure p_update_hplayoutcode
as 
type  ref_cursor  is ref cursor;
t_cur ref_cursor;
s_id hpbaselayout.id%type;
s_layoutcode hpbaselayout.layoutcode%type;
s_layoutimage hpbaselayout.layoutimage%type;
v_sql varchar2(1000);  
begin  
  v_sql := 'select id,layoutcode,layoutimage from hpbaselayout';
  open t_cur for v_sql;
  loop
    fetch  t_cur into s_id,s_layoutcode,s_layoutimage;
    update pagelayout set layoutcode=s_layoutcode,layoutimage=s_layoutimage where  id = s_id; 
    exit when  t_cur %notfound;
  end loop;
  close t_cur;
  commit;
end;
;
call p_update_hplayoutcode()
;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容