我正在将SQL Server 2005数据库转换为MySQL并且存储过程出现问题。我是MySQL存储过程的新手,所以我确定我的转换存在问题,但我没有看到它。
存储过程应该生成一个临时表,用于填充vb.net应用程序中的数据网格视图。但是,我收到错误“数据无数据 - 提取,选择或处理零行”。看起来很简单但是如果我只是将它作为查询运行,那么存储过程中的select过程将获取数据,这就是为什么我不理解错误的原因。
我真的希望有人可以告诉我为什么,因为我有几百个存储过程要转换,我在第一个就遇到了这个问题。
以下是存储过程:
DELIMITER $$
DROP PROCEDURE IF EXISTS `usp_get_unassigned_media`$$
CREATE DEFINER=`showxx`@`67.111.11.110` PROCEDURE `usp_get_unassigned_media`()
BEGIN
/* GET CURSOR WITH LOCAL LOCATIONS */
DECLARE intKey INT;
DECLARE dteDateInserted DATETIME;
DECLARE vchIdField VARCHAR(200);
DECLARE vchValueField VARCHAR(200);
DECLARE intLastKey INT;
/*TAKE OUT SPECIFIC PLAYLIST ITEMS IF TOO SLOW*/
DECLARE csrMediaToBeAssigned CURSOR FOR
SELECT
`media`.`key` AS `key`,
`media`.`date_inserted` AS `date_inserted`,
`media_detail_types`.`id` AS `id`,
`media_details`.`value` AS `value`
FROM (`media`
LEFT JOIN (`media_detail_types`
JOIN `media_details`
ON ((`media_detail_types`.`key` = `media_details`.`detail_key`)))
ON ((`media_details`.`media_key` = `media`.`key`)))
WHERE ((`media`.`is_assigned` = 0)
AND ((`media_detail_types`.`id` = 'Volume Name')
OR (`media_detail_types`.`id` = 'Drive Id')))
ORDER BY `media`.`key`,`media`.`date_inserted`,`media_detail_types`.`id`;
OPEN csrMediaToBeAssigned;
DROP TEMPORARY TABLE IF EXISTS temp_unassigned_media;
CREATE TEMPORARY TABLE temp_unnassigned_media
(temp_key INT, DateInserted DATETIME, IdField VARCHAR(200), ValueField VARCHAR (200))
ENGINE=MEMORY;
SET intLastKey = 0;
/*--GET FIRST RECORD */
FETCH FROM csrMediaToBeAssigned
INTO intKey, dteDateInserted, vchIdField, vchValueField;
/*--LOOP THROUGH CURSOR */
WHILE intLastKey = 0 DO
/*--DATA SHOULD BE IN DRIVE ID THEN VOLUME NAME */
INSERT INTO temp_unnassigned_media
VALUES (intKey, dteDateInserted, vchValueField, '');
FETCH NEXT FROM csrMediaToBeAssigned
INTO intKey, dteDateInserted, vchIdField, vchValueField;
UPDATE temp_unnassigned_media
SET IdField = vchValueField
WHERE temp_key = temp_key;
FETCH NEXT FROM csrMediaToBeAssigned
INTO intKey, dteDateInserted, vchIdField, vchValueField;
END WHILE;
SELECT *
FROM temp_unnassigned_media
ORDER BY date_inserted;
CLOSE csrMediaToBeAssigned;
/*DEALLOCATE csrMediaToBeAssigned */
/*DROP TABLE #temp_unnassigned_media */
END$$
DELIMITER ;
答案 0 :(得分:0)
你永远不会遇到WHILE循环退出的情况;初始化intLastKey变量,但它永远不会更改,因此您将获取整个结果集。在最后一条记录之后再次获取时会抛出异常。
规范模式是声明CONTINUE HANDLER
,MySQL将在触发NOT FOUND
条件时执行。处理程序通常用于设置变量,然后您可以对其进行测试,以便知道何时退出循环。
在您的情况下,在DECLARE CURSOR
语句之后和OPEN
语句之前添加此行似乎就足够了:
DECLARE CONTINUE HANDLER FOR NOT FOUND SET intLastKey = 1;