在循环中使用mysql prepared语句在执行第一个循环/循环后中断循环

2w3rbyxf  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(338)

我尝试动态访问表名,有很多表,但它只处理第一个表和循环中断。你知道我会做错什么吗

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过程中,准备好的语句是否不能在循环中工作。

j8ag8udp

j8ag8udp1#

这两条线是相反的。

END LOOP accounts_loop;

SET l_done=0;

你需要重新设置 l_done 回到 FALSE (或0)之前 END LOOP --因为 SELECT 之后的查询 FETCH can(不一定是arilu will——但是can)也会触发 CONTINUE HANDLER 设置为 TRUE (1) 错误指示光标已用完。

相关问题