在执行第一个循环/循环

时间:2019-01-06 16:35:24

标签: mysql stored-procedures

Am试图动态访问表名,并且有很多表,但是它仅处理第一个表,并且循环中断。知道我可能做错了什么

 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 个答案:

答案 0 :(得分:0)

这两行相反。

END LOOP accounts_loop;

SET l_done=0;

您需要在{em> l_done之前将FALSE重置为END LOOP(或0)–因为SELECT在查询之后 FETCH可以(不一定,但可以)也会触发CONTINUE HANDLER将其设置为{{1} }(1)错误指示光标已耗尽。