BEGIN
DECLARE sqlStr1 VARCHAR(2000);
DECLARE i INT(10) DEFAULT 0;
DECLARE k INT(10) DEFAULT 0;
DECLARE seltable VARCHAR(2000);
DECLARE _mon VARCHAR(20);
DECLARE _bh VARCHAR(20);
DECLARE _xm VARCHAR(100);
DECLARE _bm VARCHAR(20);
DECLARE _gw VARCHAR(20);
DECLARE _jj VARCHAR(20);
DECLARE Done INT DEFAULT 0;
DECLARE rs CURSOR FOR SELECT a.SYBH,a.XM,a.BMBH,a.GW from LQL170 a ;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET Done = 1;
DROP TABLE IF EXISTS tab_Ltext;
SET seltable='';
SET sqlStr1='';
SET @sqlStr1 = CONCAT('CREATE TABLE tab_Ltext(month1 VARCHAR(20), bh VARCHAR(10),xm VARCHAR(100),bm VARCHAR(20),gw VARCHAR(100),jj VARCHAR(10))');
PREPARE s FROM @sqlStr1;
EXECUTE s;项目可行性研究报告
OPEN rs;
REPEAT
IF NOT Done THEN
FETCH NEXT FROM rs INTO _bh,_xm ,_bm,_gw ;
INSERT INTO tab_Ltext(month1,bh,xm,bm,gw,jj) VALUES('2012/10',_bh,_xm,_bm,_gw,'1000');
END IF;
UNTIL Done END REPEAT;
CLOSE rs;
SELECT * FROM tab_Ltext;
END
请各位大哥帮看看~ 存储过程写的数据能出来,但是好像是循环多循环了一次,每次都是表最后一条数据出现两次,如果把FETCH NEXT FROM rs INTO _bh,_xm ,_bm,_gw ; 放在 INSERT 后面 那么第一条数据就是空的,有大神可以解决下吗?
去掉REPET UNTIL
OPEN rs;
FETCH NEXT FROM rs INTO _bh,_xm ,_bm,_gw ;
WHILE DONE=0 DO
INSERT INTO tab_Ltext(month1,bh,xm,bm,gw,jj) VALUES(strRQ,_bh,_xm,_bm,_gw,'1000');
FETCH NEXT FROM rs INTO _bh,_xm ,_bm,_gw ;
END WHILE;
CLOSE rs;