现在,我正在尝试循环一个临时表,创建了一个导入过程。我尝试过使用游标,但必须将tablename传递给存储过程。
CREATE PROCEDURE `do_update`(tablename VARCHAR(100))
BEGIN
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE c_ean VARCHAR(20);
DECLARE c_sku VARCHAR(20);
DECLARE c_mpn VARCHAR(20);
DECLARE c_manufacturerName VARCHAR(100);
DECLARE c_manufacturerUniqueId VARCHAR(100);
DECLARE c_images TEXT;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done := TRUE;
-- move declare cursor into sql to be executed
SET @sqlstatement = CONCAT('DECLARE import_cursor CURSOR FOR SELECT ean, sku, mpn, manufacturerName, manufacturerUniqueId, images FROM ', tablename);
PREPARE stmt FROM @sqlstatement;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
OPEN import_cursor;
importloop: LOOP
FETCH import_cursor INTO c_ean, c_sku, c_mpn, c_manufacturerName, c_manufacturerUniqueId, c_images;
IF done THEN
LEAVE importloop;
END IF;
CALL upsert_article(c_ean, c_sku, c_mpn, c_manufacturerName, c_manufacturerUniqueId, c_images);
END LOOP testloop;
CLOSE import_cursor;
END
注意:此导入脚本将在一天内多次调用,并且可以同时运行。因此,定义的表名不是一个选项。
到目前为止我尝试了什么:
那么,有没有办法使用游标或变通方法来完成这项工作?
答案 0 :(得分:0)
创建一个临时表并插入我的动态查询结果最后就成了诀窍。 有了这个我能够使用mysql游标与静态命名临时表。由于它是一个临时表,因此多个导入作业不会相互干扰,因为它们对其他会话是隐藏的。
最后这是代码,对我有用:
CREATE PROCEDURE `do_update`(IN
tablename VARCHAR(100))
BEGIN
DECLARE done INT;
DECLARE c_ean VARCHAR(20);
DECLARE c_sku VARCHAR(20);
DECLARE c_mpn VARCHAR(20);
DECLARE c_manufacturerName VARCHAR(100);
DECLARE c_manufacturerUniqueId VARCHAR(100);
DECLARE c_images NVARCHAR(3000);
DECLARE import_cursor CURSOR FOR SELECT ean, sku, mpn, manufacturerName, manufacturerUniqueId, images FROM importjob;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done := TRUE;
DROP TEMPORARY TABLE IF EXISTS importjob;
SET @sqlstatement = CONCAT('CREATE TEMPORARY TABLE importjob SELECT ean, sku, mpn, manufacturerName, manufacturerUniqueId, images FROM ', tablename);
PREPARE stmt FROM @sqlstatement;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET @DEBUG = true;
OPEN import_cursor;
importloop: LOOP
FETCH import_cursor INTO c_ean, c_sku, c_mpn, c_manufacturerName, c_manufacturerUniqueId, c_images;
IF done THEN
LEAVE importloop;
END IF;
CALL upsert_article(c_ean, c_sku, c_mpn, c_manufacturerName, c_manufacturerUniqueId, c_images);
END LOOP importloop;
CLOSE import_cursor;
DROP TEMPORARY TABLE IF EXISTS importjob;
END