MySQL游标只返回一行

时间:2014-03-14 05:10:13

标签: mysql

这里我想要变量ISSUDAYS中的20条记录。但我的光标只返回第一个reord。 issue_book.issue_date列包含20条记录..!请帮我。提前谢谢你。

DECLARE RESULTDAYS,ISSUEDAYS,TODAYS DATE;
DECLARE done VARCHAR(10);
DECLARE DAYS INT DEFAULT 1;
DECLARE CUR1 CURSOR FOR SELECT issue_book.issue_date FROM bookstore.issue_book WHERE   return_date IS NULL AND status = 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=TRUE;       
OPEN CUR1;
READ_LOOP: LOOP
 FETCH CUR1 INTO ISSUEDAYS;
 IF done THEN
 LEAVE READ_LOOP;
 END IF;
  SELECT ISSUEDAYS;
 END LOOP;
CLOSE CUR1;

2 个答案:

答案 0 :(得分:3)

尝试创建临时表并插入。然后在光标完成后从临时表中查询一次。

BEGIN
DECLARE RESULTDAYS,ISSUEDAYS,TODAYS DATE;
DECLARE done BOOL DEFAULT FALSE;
DECLARE DAYS INT DEFAULT 1;
DECLARE CUR1 CURSOR FOR SELECT issue_book.issue_date FROM bookstore.issue_book WHERE return_date IS NULL AND status = 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=TRUE;

DROP TABLE IF EXISTS mytemptable;
CREATE TEMPORARY TABLE IF NOT EXISTS mytemptable(issue_date DATE);

OPEN CUR1;

READ_LOOP: LOOP
FETCH CUR1 INTO ISSUEDAYS;
IF done THEN
LEAVE READ_LOOP;
END IF;

INSERT INTO mytemptable SELECT ISSUEDAYS;
END LOOP;
CLOSE CUR1;
SELECT * FROM mytemptable;
END

答案 1 :(得分:0)

是的,这里显示光标所需的所有记录需要创建一个临时表..    宣布结束日,即将到来,今天;

DECLARE DAYS INT DEFAULT 1;
DECLARE COUNTER, done INT DEFAULT FALSE;
DECLARE CUR1 CURSOR FOR SELECT issue_book.issue_date FROM bookstore.issue_book WHERE return_date IS NULL AND status = 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=TRUE;  
CREATE TEMPORARY TABLE temp
(
id int not null auto_increment primary key, 
tempdate DATE
);
OPEN CUR1;
READ_LOOP: LOOP
FETCH CUR1 INTO ISSUEDAYS;
SET COUNTER=COUNTER+1;
IF done THEN LEAVE READ_LOOP;
END IF;
  INSERT INTO temp(tempdate)
  SELECT ISSUEDAYS AS tempdate;
    END LOOP;
CLOSE CUR1;
SELECT * FROM temp;
DROP TABLE temp;