代码需求:业务更改状态时写通知表,通知用户。但是报错。各位大牛帮看下呗?
BEGIN declare main_id,business_id,people_count, msg_id,launch_user_id int; DECLARE business_type,msg_type CHAR(2); declare _DONE TINYINT(1) default 0; /*能否达到记录的末尾控制变量*/ DECLARE regists CURSOR FOR select r.main_id, ""02"" as business_type, ""02"" as msg_type, r.id as business_id , r.launch_user_id, (SELECT count(DISTINCT la.wz_regist_user.user_id) FROM wz_regist_options JOIN wz_regist_user ON la.wz_regist_options.id = la.wz_regist_user.option_id WHERE la.wz_regist_options.regist_id = r.id) AS people_count FROM wz_regist r where r.cutoff_datetime<=now() and r.status=""01""; DECLARE CONTINUE HANDLER FOR NOT FOUND SET _DONE = 1; OPEN regists; LOOP_OUTER:LOOP FETCH regists INTO main_id, business_type, msg_type, business_id, launch_user_id, people_count; IF _DONE =1 THEN LEAVE LOOP_OUTER; END IF; BEGIN update wz_regist r set r.status=""03"" where r.id = business_id; insert into wz_user_message(main_id, title, content, msg_type, value_datetime, business_id, business_type) values(main_id, ""系统通知"", concat(""您发起的报名已截止,共有"",people_count,""人参与报名。""), msg_type, now(), business_id, business_type); select LAST_INSERT_ID() into msg_id; IF launch_user_id is not NULL THEN INSERT INTO wz_user_message_receive(message_id, wz_user_id, receive_datetime, is_read) VALUES (msg_id, launch_user_id, now(), ""0""); end IF; IF people_count >0 THEN insert into wz_user_message(main_id, title, content, msg_type, value_datetime, business_id, business_type) values(main_id, ""系统通知"", concat(""您参与的报名已截止,共有"",people_count,""人参与报名。""), msg_type, now(), business_id, business_type); DECLARE msg_id_INNER int; select LAST_INSERT_ID() into msg_id_INNER; declare _inner tinyint(1) default 0; DECLARE joiner_id int(11); declare continue handler for not found set _inner = 1; DECLARE regist_joiners CURSOR FOR SELECT DISTINCT wz_regist_user.user_id as joiner_id FROM wz_regist_options JOIN wz_regist_user ON wz_regist_options.id = wz_regist_user.option_id WHERE wz_regist_options.regist_id = business_id; OPEN regist_joiners; LOOP_INNER:LOOP FETCH regist_joiners INTO joiner_id; if _inner=1 then leave LOOP_INNER; end if; INSERT INTO wz_user_message_receive(message_id, wz_user_id, receive_datetime, is_read) VALUES (msg_id, joiner_id, now(), ""0""); END LOOP; COMMIT; END IF; END; END LOOP; END
解决方案:30分
把错误也贴下。
解决方案:70分
注意红字部分。
BEGIN
declare main_id,business_id,people_count, msg_id,launch_user_id int;
DECLARE business_type,msg_type CHAR(2);
declare _DONE TINYINT(1) default 0; /*能否达到记录的末尾控制变量*/
declare _DONE_O TINYINT;
DECLARE regists CURSOR FOR select r.main_id, “”02″” as business_type, “”02″” as msg_type, r.id as business_id , r.launch_user_id, (SELECT count(DISTINCT la.wz_regist_user.user_id) FROM wz_regist_options JOIN wz_regist_user ON la.wz_regist_options.id = la.wz_regist_user.option_id WHERE la.wz_regist_options.regist_id = r.id) AS people_count FROM wz_regist r where r.cutoff_datetime<=now() and r.status=””01″”;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET _DONE = 1;
OPEN regists;
LOOP_OUTER:LOOP
FETCH regists INTO main_id, business_type, msg_type, business_id, launch_user_id, people_count;
IF _DONE =1 THEN
LEAVE LOOP_OUTER;
END IF;
BEGIN
set _DONE_O=_DONE;
update wz_regist r set r.status=””03″” where r.id = business_id;
insert into wz_user_message(main_id, title, content, msg_type, value_datetime, business_id, business_type) values(main_id, “”系统通知””, concat(“”您发起的报名已截止,共有””,people_count,””人参与报名。””), msg_type, now(), business_id, business_type);
select LAST_INSERT_ID() into msg_id;
IF launch_user_id is not NULL THEN
INSERT INTO wz_user_message_receive(message_id, wz_user_id, receive_datetime, is_read) VALUES (msg_id, launch_user_id, now(), “”0″”);
end IF;
IF people_count >0 THEN
insert into wz_user_message(main_id, title, content, msg_type, value_datetime, business_id, business_type) values(main_id, “”系统通知””, concat(“”您参与的报名已截止,共有””,people_count,””人参与报名。””), msg_type, now(), business_id, business_type);
DECLARE msg_id_INNER int;
select LAST_INSERT_ID() into msg_id_INNER;
declare _inner tinyint(1) default 0;
DECLARE joiner_id int(11);
declare continue handler for not found set _inner = 1;
DECLARE regist_joiners CURSOR FOR SELECT DISTINCT wz_regist_user.user_id as joiner_id FROM wz_regist_options JOIN wz_regist_user ON wz_regist_options.id = wz_regist_user.option_id WHERE wz_regist_options.regist_id = business_id;
OPEN regist_joiners;
LOOP_INNER:LOOP
FETCH regist_joiners INTO joiner_id;
if _inner=1 then
leave LOOP_INNER;
end if;
INSERT INTO wz_user_message_receive(message_id, wz_user_id, receive_datetime, is_read) VALUES (msg_id, joiner_id, now(), “”0″”);
END LOOP;
COMMIT;
END IF;
set _DONE=_DONE_O;
END;
END LOOP;
END
BEGIN
declare main_id,business_id,people_count, msg_id,launch_user_id int;
DECLARE business_type,msg_type CHAR(2);
declare _DONE TINYINT(1) default 0; /*能否达到记录的末尾控制变量*/
declare _DONE_O TINYINT;
DECLARE regists CURSOR FOR select r.main_id, “”02″” as business_type, “”02″” as msg_type, r.id as business_id , r.launch_user_id, (SELECT count(DISTINCT la.wz_regist_user.user_id) FROM wz_regist_options JOIN wz_regist_user ON la.wz_regist_options.id = la.wz_regist_user.option_id WHERE la.wz_regist_options.regist_id = r.id) AS people_count FROM wz_regist r where r.cutoff_datetime<=now() and r.status=””01″”;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET _DONE = 1;
OPEN regists;
LOOP_OUTER:LOOP
FETCH regists INTO main_id, business_type, msg_type, business_id, launch_user_id, people_count;
IF _DONE =1 THEN
LEAVE LOOP_OUTER;
END IF;
BEGIN
set _DONE_O=_DONE;
update wz_regist r set r.status=””03″” where r.id = business_id;
insert into wz_user_message(main_id, title, content, msg_type, value_datetime, business_id, business_type) values(main_id, “”系统通知””, concat(“”您发起的报名已截止,共有””,people_count,””人参与报名。””), msg_type, now(), business_id, business_type);
select LAST_INSERT_ID() into msg_id;
IF launch_user_id is not NULL THEN
INSERT INTO wz_user_message_receive(message_id, wz_user_id, receive_datetime, is_read) VALUES (msg_id, launch_user_id, now(), “”0″”);
end IF;
IF people_count >0 THEN
insert into wz_user_message(main_id, title, content, msg_type, value_datetime, business_id, business_type) values(main_id, “”系统通知””, concat(“”您参与的报名已截止,共有””,people_count,””人参与报名。””), msg_type, now(), business_id, business_type);
DECLARE msg_id_INNER int;
select LAST_INSERT_ID() into msg_id_INNER;
declare _inner tinyint(1) default 0;
DECLARE joiner_id int(11);
declare continue handler for not found set _inner = 1;
DECLARE regist_joiners CURSOR FOR SELECT DISTINCT wz_regist_user.user_id as joiner_id FROM wz_regist_options JOIN wz_regist_user ON wz_regist_options.id = wz_regist_user.option_id WHERE wz_regist_options.regist_id = business_id;
OPEN regist_joiners;
LOOP_INNER:LOOP
FETCH regist_joiners INTO joiner_id;
if _inner=1 then
leave LOOP_INNER;
end if;
INSERT INTO wz_user_message_receive(message_id, wz_user_id, receive_datetime, is_read) VALUES (msg_id, joiner_id, now(), “”0″”);
END LOOP;
COMMIT;
END IF;
set _DONE=_DONE_O;
END;
END LOOP;
END