MYSQL建立包含游标的存储过程,提示
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE dbCur CURSOR FOR SELECT asset_no,model,`USER`,location FROM  itsm_asset_' at line 9完整的存储过程代码:CREATE PROCEDURE sp_harddisk_asset_inventory(inventoryId int)
BEGIN
  DECLARE done INT DEFAULT FALSE;
  DECLARE i_sourceType,i_assetNo,i_model,i_userId,i_location VARCHAR(255);
  DECLARE d_assetNo,d_model,d_userId,d_location VARCHAR(255);
  DECLARE _model_flag,_userId_flag,_location_flag int 
  DECLARE dbCur CURSOR FOR SELECT asset_no,model,`USER`,location FROM itsm_asset_new where asset_no in ( select asset_no from itsm_asset_source where inventory_id=inventoryId);
  DECLARE importCur CURSOR FOR SELECT `source_type`,`asset_no`,`model`,`user_id`,`location` FROM `itsm_asset_source` WHERE `inventory_id`=inventoryId AND `asset_No` IN (SELECT `asset_No` FROM `itsm_asset_new`);
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;  OPEN importCur;
  OPEN dbCur;  read_loop: LOOP
    FETCH importCur INTO i_sourceType,i_asetNo,i_model,i_userId,i_location;
    FETCH dbCur INTO d_asetNo,d_model,d_userId,d_location;
    IF done THEN
      LEAVE read_loop;
    END IF;
    IF i_asetNo=d_asetNo THEN
IF i_model=d_model then set model_flag=0; else set model_flag=1 end;
IF i_userId=d_userId then set _userId_flag=0; else set _userId_flag=1;
IF i_location=d_location then set _location_flag=0; else set _location_flag=1; INSERT into itsm_asset_inventory_report (inventory_id,report_type,source,asset_no,model,model_flag,user_name,user_name_flag,location,location_flag) 
VALUES (inventoryId,2,i_sourceType,i_asetNo,i_model,_model_flag,i_userId,_userId_flag,i_location,_location_flag);
    
    END IF;
  END LOOP;  CLOSE importCur;
  CLOSE dbCur;
END;不会用游标,只是对照MYSQL手册写的。不知道哪写的不对。
参考链接:http://dev.mysql.com/doc/refman/5.6/en/cursors.html
麻烦告诉我怎么修改,或者告诉我参考的文档。谢谢。mysql

解决方案 »

  1.   

    DELIMITER $$
    CREATE PROCEDURE sp_harddisk_asset_inventory(inventoryId INT)
    BEGIN
      DECLARE done INT DEFAULT FALSE;
      DECLARE i_sourceType,i_asetNo,i_model,i_userId,i_location VARCHAR(255);
      DECLARE d_asetNo,d_model,d_userId,d_location VARCHAR(255);
      DECLARE model_flag,_userId_flag,_location_flag INT;
     
      
      DECLARE dbCur CURSOR FOR SELECT asset_no,model,`USER`,location FROM itsm_asset_new WHERE asset_no IN ( SELECT asset_no FROM itsm_asset_source WHERE inventory_id=inventoryId);
      DECLARE importCur CURSOR FOR SELECT `source_type`,`asset_no`,`model`,`user_id`,`location` FROM `itsm_asset_source` WHERE `inventory_id`=inventoryId AND `asset_No` IN (SELECT `asset_No` FROM `itsm_asset_new`);
      DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
     
      OPEN importCur;
      OPEN dbCur;
     
      read_loop: LOOP
        FETCH importCur INTO i_sourceType,i_asetNo,i_model,i_userId,i_location;
        FETCH dbCur INTO d_asetNo,d_model,d_userId,d_location;
        IF done THEN
          LEAVE read_loop;
        END IF;
        IF i_asetNo=d_asetNo THEN
        SET model_flag=IF(i_model=d_model,0,1);
        SET _userId_flag=IF(i_userId=d_userId,0,1);
        SET _location_flaG=IF(i_location=d_location,0,1);
     
            INSERT INTO itsm_asset_inventory_report (inventory_id,report_type,source,asset_no,model,model_flag,user_name,user_name_flag,location,location_flag) 
                VALUES (inventoryId,2,i_sourceType,i_asetNo,i_model,_model_flag,i_userId,_userId_flag,i_location,_location_flag);
         
        END IF;
      END LOOP;
     
      CLOSE importCur;
      CLOSE dbCur;
    END;$$
    DELIMITER ;