CREATE PROCEDURE pro_nogoodsother(num int , varchar(200),memberId INT,consumerId INT,p INT, oncePoint INT,comAmout INT ,canPoint INT )   
BEGIN   
  DECLARE id INT ;   
  DECLARE lnumber INT ;   
  DECLARE STOP INT DEFAULT 0;   
  DECLARE cnt INT DEFAULT 0;   
  DECLARE tempId INT DEFAULT 0;  
 
  SELECT loopNumber INTO lnumber FROM tbl_loop;   
  while num>0 do   
           DECLARE     cur CURSOR FOR SELECT goodsPointId FROM nogoodspointother ORDER BY goodsPointId ASC ;   
          DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET STOP=1;   
          OPEN cur;     
           FETCH cur INTO id ;   
          WHILE STOP <> 1 DO   
          SELECT COUNT(*) INTO cnt FROM nogoodspointother WHERE goodsPointId= id+1;
          IF cnt=0 AND id>=tempId THEN  
          BEGIN  
          select id;  
          select tempId;
         INSERT INTO nogoodspointOther(goodsPointId,goodsId,memberId,consumerId,canPoints,totalPoints)  
         VALUES (id+1,goodsId,memberId,consumerId,canPoints,oncePoint);   
         SET p=p-oncePoint;
         SET comAmout=comAmout-canPoint;   
         SET tempId=id+ lnumber;
          select tempId;
          END;   
          END IF ;
          FETCH cur INTO id;   
          END WHILE;    
   CLOSE cur;     
  set num=num-1 ;
end while ;
 END;
这上面怎么不对呢?while循环里怎么添加游标?while 里好像有 declare 就报错!

解决方案 »

  1.   

    DELIMITER $$
    DROP PROCEDURE IF EXISTS pro_nogoodsother $$
    CREATE PROCEDURE pro_nogoodsother(num INT , aa VARCHAR(200),memberId INT,consumerId INT,p INT, oncePoint INT,comAmout INT ,canPoint INT )   
    BEGIN   
      DECLARE id INT ;   
      DECLARE lnumber INT ;   
      DECLARE STOP INT DEFAULT 0;   
      DECLARE cnt INT DEFAULT 0;   
      DECLARE tempId INT DEFAULT 0;  
      DECLARE cur CURSOR FOR SELECT goodsPointId FROM nogoodspointother ORDER BY goodsPointId ASC ;   
              DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET STOP=1; 
      SELECT loopNumber INTO lnumber FROM tbl_loop;   
      WHILE num>0 DO   
               
              OPEN cur;     
               FETCH cur INTO id ;   
              WHILE STOP <> 1 DO   
              SELECT COUNT(*) INTO cnt FROM nogoodspointother WHERE goodsPointId= id+1;
              IF cnt=0 AND id>=tempId THEN  
              BEGIN  
              SELECT id;  
              SELECT tempId;
             INSERT INTO nogoodspointOther(goodsPointId,goodsId,memberId,consumerId,canPoints,totalPoints)  
             VALUES (id+1,goodsId,memberId,consumerId,canPoints,oncePoint);   
             SET p=p-oncePoint;
             SET comAmout=comAmout-canPoint;   
             SET tempId=id+ lnumber;
              SELECT tempId;
              END;   
              END IF ;
              FETCH cur INTO id;   
              END WHILE;    
       CLOSE cur;     
      SET num=num-1 ;
    END WHILE ;
     END; $$
    DELIMITER ;
    num int , varchar(200) 少1个参数
      

  2.   

    在declare之前需要有begin 开头,
      

  3.   

    DECLARE cur CURSOR FOR SELECT goodsPointId FROM nogoodspointother ORDER BY goodsPointId ASC ;   
    这不能放到外面,我要这里加条件,条件是循环取出来的值。
      

  4.   

    什么条件,MYSQL不支持动态游标
      

  5.   

    改为如下即可。CREATE PROCEDURE pro_nogoodsother(num int , varchar(200),memberId INT,consumerId INT,p INT, oncePoint INT,comAmout INT ,canPoint INT )   
    BEGIN   
      DECLARE id INT ;   
      DECLARE lnumber INT ;   
      DECLARE STOP INT DEFAULT 0;   
      DECLARE cnt INT DEFAULT 0;   
      DECLARE tempId INT DEFAULT 0;             DECLARE     cur CURSOR FOR SELECT goodsPointId FROM nogoodspointother ORDER BY goodsPointId ASC ;   
              DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET STOP=1;   
     
      SELECT loopNumber INTO lnumber FROM tbl_loop;   
      while num>0 do   
              OPEN cur;     
               FETCH cur INTO id ;   
              WHILE STOP <> 1 DO   
              SELECT COUNT(*) INTO cnt FROM nogoodspointother WHERE goodsPointId= id+1;
              IF cnt=0 AND id>=tempId THEN  
              BEGIN  
              select id;  
              select tempId;
             INSERT INTO nogoodspointOther(goodsPointId,goodsId,memberId,consumerId,canPoints,totalPoints)  
             VALUES (id+1,goodsId,memberId,consumerId,canPoints,oncePoint);   
             SET p=p-oncePoint;
             SET comAmout=comAmout-canPoint;   
             SET tempId=id+ lnumber;
              select tempId;
              END;   
              END IF ;
              FETCH cur INTO id;   
              END WHILE;    
       CLOSE cur;     
      set num=num-1 ;
    end while ;
     END;
      

  6.   

    放到外面不影响你加条件。 游标可以动态进行WHERE条件查询
      

  7.   


     
    CREATE PROCEDURE pro_nogoodsother(num int , varchar(200),memberId INT,consumerId INT,p INT, oncePoint INT,comAmout INT ,canPoint INT )   
    BEGIN   
      DECLARE id INT ;   
      DECLARE lnumber INT ;   
      DECLARE STOP INT DEFAULT 0;   
      DECLARE cnt INT DEFAULT 0;   
      DECLARE tempId INT DEFAULT 0;  
     
      SELECT loopNumber INTO lnumber FROM tbl_loop;   
      while num>0 do            DECLARE   cur CURSOR FOR SELECT goodsPointId FROM nogoodspointother  where goodsPointId  >tempId ORDER BY goodsPointId ASC ;   
              DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET STOP=1;   
              OPEN cur;     
               FETCH cur INTO id ;   
              WHILE STOP <> 1 DO   
              SELECT COUNT(*) INTO cnt FROM nogoodspointother WHERE goodsPointId= id+1;
             IF cnt=0 AND id>=tempId THEN  
              BEGIN  
              select id;  
              select tempId;
             INSERT INTO nogoodspointOther(goodsPointId,goodsId,memberId,consumerId,canPoints,totalPoints)  
             VALUES (id+1,goodsId,memberId,consumerId,canPoints,oncePoint);   
             SET p=p-oncePoint;
             SET comAmout=comAmout-canPoint;   
             SET tempId=id+ lnumber;
              select tempId;
              END;   
              END IF ;
              FETCH cur INTO id;   
              END WHILE;  
         CLOSE cur;     
      set num=num-1 ;  end while ;
     END;  
     DECLARE   cur CURSOR FOR SELECT goodsPointId FROM nogoodspointother  where goodsPointId  >tempId ORDER BY goodsPointId ASC ; tempId 是在循环里取的,放到外面值不变了。 
      

  8.   

    DECLARE   cur CURSOR FOR SELECT * from newtt   
    DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET STOP=1;   
    SET @asql='create table newtt as goodsPointId FROM nogoodspointother  where goodsPointId  >tempId';
     PREPARE stml FROM @asql;
     EXECUTE stml;
    OPEN cur;     
    FETCH cur INTO id ;......or在循环中判断
      

  9.   

     ---------------------------
    错误
    ---------------------------
    SQL 执行错误 # 1054. 从数据库的响应:Unknown column 'tempId' in 'where clause'
    ---------------------------
    确定   
    ---------------------------
    这是怎么回事?执行的时候的错。
      

  10.   

    SET @asql=CONCAT('create table newtt as goodsPointId FROM nogoodspointother where goodsPointId >',tempId);
     PREPARE stml FROM @asql;
     EXECUTE stml;