尝试嵌套游标时在MySQL存储过程中获取语法错误
我有一个MySQL存储过程脚本,试图在其中实现嵌套的游标。下面是我的代码尝试嵌套游标时在MySQL存储过程中获取语法错误,mysql,sql,stored-procedures,Mysql,Sql,Stored Procedures,我有一个MySQL存储过程脚本,试图在其中实现嵌套的游标。下面是我的代码 DELIMITER // DROP PROCEDURE IF EXISTS PopulateStaleEntityTable// DELETE FROM bucket_stale; LOAD DATA LOCAL INFILE '/home/centos/mysql1.txt' INTO TABLE bucket_stale; CREATE PROCEDURE PopulateStaleEntityTable() B
DELIMITER //
DROP PROCEDURE IF EXISTS PopulateStaleEntityTable//
DELETE FROM bucket_stale;
LOAD DATA LOCAL INFILE '/home/centos/mysql1.txt' INTO TABLE bucket_stale;
CREATE PROCEDURE PopulateStaleEntityTable()
BEGIN
DECLARE finished INTEGER DEFAULT 0;
DECLARE finished_stale INTEGER DEFAULT 0;
DECLARE product_offer_id_var bigint(20) DEFAULT 0;
DECLARE product_offer_group_id_var bigint(20) DEFAULT 0;
DECLARE batch_count INTEGER DEFAULT 0;
DECLARE max_batch_count INTEGER DEFAULT 100;
DECLARE bucket_id_var bigint(10) DEFAULT 0;
DECLARE stale_cursor CURSOR FOR
SELECT staleid FROM bucket_stale;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished_stale = 1;
OPEN stale_cursor;
insert_stale : LOOP
FETCH stale_cursor INTO bucket_id_var;
SELECT bucket_id_var;
BEGIN
DECLARE product_cursor CURSOR FOR
SELECT product_offer_id FROM product where bucket_id='1086';
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished = 1;
OPEN product_cursor;
insert_entity: LOOP
START TRANSACTION;
FETCH product_cursor INTO product_offer_id_var;
SELECT product_offer_group_id INTO product_offer_group_id_var FROM product_offer where id=product_offer_id_var;
INSERT IGNORE INTO stale_cached_entity (entity_type,entity_id,cache_action,created,field_change) VALUES ('PRODUCT_OFFER_GROUP',product_offer_group_id_var, 'UPDATE',now(),'DEFAULT_SUPC_LIST1');
SET batch_count=batch_count+1;
IF batch_count > max_batch_count THEN
COMMIT;
SET batch_count=0;
END IF;
IF finished = 1 THEN
LEAVE insert_entity;
END IF;
END LOOP insert_entity
END;
IF finished_stale = 1 THEN
LEAVE insert_stale;
END IF;
END LOOP insert_stale;
CLOSE stale_cursor;
END//
DELIMITER ;
当我尝试运行此脚本时,它会出现以下错误
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 'END;
IF finished_stale = 1 THEN
LEAVE insert_stale;
END IF; ' at line 38
我试着找出问题出在哪里,但我做不到。我遗漏了什么?问题在于这一行:
END LOOP insert_entity
如果在查看游标代码的示例时,您会看到END循环不需要标签,它需要分号语句终止符
像这样:
END LOOP;
我同意@tadman的评论。我知道一些有Oracle或Microsoft经验的开发人员希望使用存储过程,但坦率地说,MySQL的存储过程支持糟糕透顶,编写一些简单的Python脚本或任何其他语言都会让您更开心、更高效。如此多的SQL表明您正试图在数据库中执行应用程序级操作。为什么这需要在内部完成?