BEGIN #Routine body goes here... DECLARE vec_title VARCHAR(50) DEFAULT ""; DECLARE vec_content VARCHAR(2000) DEFAULT ""; DECLARE int_attach_id INT DEFAULT 0; DECLARE date_send_time DATETIME; DECLARE _DONE int default 0; DECLARE cur CURSOR FOR SELECT title, content, attach_id, send_time FROM all_server_mail WHERE id IN(SELECT all_server_mail_id FROM user_unreceived_mail WHERE user_id = inUserID ); DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET _DONE = 1;#错误定义,标记循环结束 OPEN cur; #插入user_mail表 REPEAT FETCH cur INTO vec_title, vec_content, int_attach_id, date_send_time; IF NOT _DONE THEN INSERT INTO `user_mail`(`user_id`, `send_time`, `title`, `content`, `attach_id`, `status`) VALUES(inUserID, date_send_time, vec_title, vec_content, int_attach_id, 0); END IF; UNTIL _DONE END REPEAT; CLOSE cur; #清空user_unreceived_mail表中该玩家记录 DELETE FROM user_unreceived_mail WHERE `user_id` = inUserID; END
终于写对了一个存储过程