mysql存储过程无法正常工作我已经给出了下面的代码请帮帮我

时间:2012-03-29 06:51:28

标签: mysql stored-procedures

这是根据杂志ID

将问题表的内容写入临时表的过程

delimiter //

CREATE PROCEDURE `magazineCursor`()
BEGIN
DECLARE d INT default 0;
DECLARE magazine_id,issue_id,temp_id INT;
DECLARE issue_name VARCHAR(20);
DECLARE cur1 CURSOR FOR SELECT  magazine_id FROM tbl_magazine limit 1 ;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET d=1;
DECLARE CONTINUE HANDLER FOR SQLSTATE '23000' SET d=1;
OPEN cur1;
lb: LOOP
    IF d=1 THEN
      LEAVE lb;
      END IF;
      IF NOT d=1 THEN
      FETCH cur1 INTO temp_id;
      SELECT magazine_id ,issue_id,issue_name   INTO magazine_id,issue_id,issue_name FROM tbl_magazine_issue  where magazine_id=temp_id and os_select=2 order by  issue_date    desc limit 1;
        insert into temp_magazine_table values(magazine_id,issue_id,issue_name);
       END IF;
      END LOOP;
      CLOSE cur1;
      END//

CREATE PROCEDURE `magazineCursor`() BEGIN DECLARE d INT default 0; DECLARE magazine_id,issue_id,temp_id INT; DECLARE issue_name VARCHAR(20); DECLARE cur1 CURSOR FOR SELECT magazine_id FROM tbl_magazine limit 1 ; DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET d=1; DECLARE CONTINUE HANDLER FOR SQLSTATE '23000' SET d=1; OPEN cur1; lb: LOOP IF d=1 THEN LEAVE lb; END IF; IF NOT d=1 THEN FETCH cur1 INTO temp_id; SELECT magazine_id ,issue_id,issue_name INTO magazine_id,issue_id,issue_name FROM tbl_magazine_issue where magazine_id=temp_id and os_select=2 order by issue_date desc limit 1; insert into temp_magazine_table values(magazine_id,issue_id,issue_name); END IF; END LOOP; CLOSE cur1; END//

1 个答案:

答案 0 :(得分:1)

在您的LOOP中,您无法检查是否已创建临时表。

您需要在某个时刻设置d = 1,而不是

这可以通过多种方式完成,但简化为

SET x = 1; // if counting rows
lb: LOOP
  ... your code ...
  if x > 10 THEN SET d = 1;
  else SET  x = x + 1;
END LOOP;