我尝试动态访问表名,有很多表,但它只处理第一个表和循环中断。你知道我会做错什么吗
PROCEDURE IF EXISTS pmms.TdevidendPaymentOnSavings;
DELIMITER //
CREATE PROCEDURE TdevidendPaymentOnSavings() READS SQL DATA BEGIN
DECLARE accountNumber VARCHAR(30);DECLARE tableName VARCHAR(30);DECLARE
theAccountDate1 DATE;
DECLARE anyDateInYear DATE;DECLARE rateUsed INTEGER;DECLARE lastDate
DATE;
DECLARE ledgerBalance1 INTEGER;DECLARE amountComputed INTEGER;DECLARE
monthlySummations INTEGER;
DECLARE monthlyTotals INTEGER DEFAULT 0; DECLARE l_done INTEGER;DECLARE
finalTotals INTEGER DEFAULT 0;
DECLARE forSelectingAccountNumbers CURSOR FOR SELECT account_number
FROM
pmms.account_created_store WHERE account_number LIKE '05502%10';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET l_done=1;
SET l_done=0;
SELECT SavingsStartDate,SavingsRateUsed INTO anyDateInYear,rateUsed from
SavingsSharesComputationParameters;
OPEN forSelectingAccountNumbers;
accounts_loop: LOOP
FETCH forSelectingAccountNumbers into accountNumber;
IF l_done=1 THEN
LEAVE accounts_loop;
END IF;
SET tableName=CONCAT('bsanca',accountNumber);
CALL accountNma(accountNumber,@accountName);
SELECT tableName,accountNumber;
SET @sql_text1 = concat('SELECT ledger_balance INTO @ledgerBalance from
',tableName,' WHERE trn_date=@theAccountDate ORDER BY trn_id DESC LIMIT
1');
SELECT @sql_text1;
SELECT @ledgerBalance ;SELECT @theAccountDate;
PREPARE stmt1 FROM @sql_text1;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
END LOOP accounts_loop;
SET l_done=0;
CLOSE forSelectingAccountNumbers;
END//
DELIMITER ;
我已经检查过无数次了,但是当我正确地删除准备好的语句和循环时,我似乎没有发现问题。
请帮助了解在mysql过程中,准备好的语句是否不能在循环中工作。
1条答案
按热度按时间j8ag8udp1#
这两条线是相反的。
你需要重新设置
l_done
回到FALSE
(或0)之前END LOOP
--因为SELECT
之后的查询FETCH
can(不一定是arilu will——但是can)也会触发CONTINUE HANDLER
设置为TRUE
(1) 错误指示光标已用完。