DELIMITER// #防止mysql遇到分号(;)就执行,换成//,后面遇到//才执行。 CREATE PROCEDURE setAccountIdFrameId() #创建名为setAccountIdFrameId存储过程 BEGIN # 声明变量 DECLARE accountids INT(11); DECLARE frameids VARCHAR(100); #定义结束标识 DECLARE done INT DEFAULT 0; # 定义一个游标来记录sql查询的结果 DECLARE cur CURSOR FOR SELECT a.accountid,f.frameid FROM nst_t_account a LEFT JOIN nst_t_accountmiddle m ON a.username= m.usr_login LEFT JOIN nst_t_frame f ON CONCAT(m.USR_UDF_SETID,m.USR_UDF_DEPTID)= f.framecode WHERE m.usr_login IS NOT NULL AND f.frameid IS NOT NULL; #定义游标的结束--当遍历完成时,将DONE设置为1 DECLARE CONTINUE HANDLER FOR SQLSTATE ‘02000‘ SET done = 1; # 打开游标 OPEN cur; # 将游标向下移1行,获取的数据放入之前定义的变量accountids,frameids中 FETCH NEXT FROM cur INTO accountids,frameids; -- 遍历游标每一行 REPEAT IF NOT done THEN UPDATE nst_t_account SET frameid=frameids WHERE accountid=accountids; END IF; # 将游标向下移1行,获取的数据放入之前定义的变量accountids,frameids中 FETCH NEXT FROM cur INTO accountids,frameids; UNTIL done END REPEAT; #关闭游标 CLOSE cur; END;//
CALL setAccountIdFrameId();//执行