创建过程中的语法错误

时间:2015-12-17 21:18:17

标签: mysql stored-procedures

使用Toad for MySQL,我在CREATE PROCEDURE语句的最后部分收到错误,其中代码为END //。这是我的完整代码:

DELIMITER //
CREATE PROCEDURE spWHS_FundedYTD()
BEGIN
DECLARE fundedyear INT;
DECLARE fundedmonth INT;

SET fundedyear = 2000;
SET fundedmonth = 1;

WHILE fundedyear <= 2015 DO

 BEGIN

   WHILE fundedmonth <= 12 DO

     BEGIN

        SELECT CONCAT(a15.LastName, ', ' ,a15.FirstName) AS AE,
               a11.LOS_Loan_Number__c,
               a11.Funded_Date__c,
               a11.Amount,
               a11.Loan_Type__c,
               a11.Purpose__c
         FROM   `Opportunity`   a11 
                LEFT JOIN Account   a12 
                  ON    (a11.AccountId = a12.Id)
                LEFT JOIN   `RecordType`    a14 
            ON  (a11.RecordTypeId = a14.Id)
                LEFT JOIN   `USER`  a15 
                  ON    (a11.OwnerId = a15.Id)
        WHERE a14.Name = 'Wholesale Loan'
            AND YEAR(a11.Funded_Date__c) = 2015
            AND MONTH(a11.Funded_Date__c) = 4
           AND a15.LastName = 'BADDE';

       SET @fundedmonth = @fundedmonth + 1;

    END WHILE;

    SET @fundedmonth = 1;
    SET @fundedyear = @fundedyear + 1;

 END WHILE;

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 'WHILE;

    SET @fundedmonth = 1;
    SET @fundedyear = @fundedyear + 1;' at line 37  45  3

我无法弄清楚为什么我会收到错误。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

在WHILE之后你不需要BEGIN。你的程序应该是:

DELIMITER //
CREATE PROCEDURE spWHS_FundedYTD()
BEGIN
DECLARE fundedyear INT;
DECLARE fundedmonth INT;

SET fundedyear = 2000;
SET fundedmonth = 1;

WHILE fundedyear <= 2015 DO

   WHILE fundedmonth <= 12 DO

        SELECT CONCAT(a15.LastName, ', ' ,a15.FirstName) AS AE,
               a11.LOS_Loan_Number__c,
               a11.Funded_Date__c,
               a11.Amount,
               a11.Loan_Type__c,
               a11.Purpose__c
         FROM   `Opportunity`   a11 
                LEFT JOIN Account   a12 
                  ON    (a11.AccountId = a12.Id)
                LEFT JOIN   `RecordType`    a14 
            ON  (a11.RecordTypeId = a14.Id)
                LEFT JOIN   `USER`  a15 
                  ON    (a11.OwnerId = a15.Id)
        WHERE a14.Name = 'Wholesale Loan'
            AND YEAR(a11.Funded_Date__c) = 2015
            AND MONTH(a11.Funded_Date__c) = 4
           AND a15.LastName = 'BADDE';

       SET @fundedmonth = @fundedmonth + 1;

    END WHILE;

    SET @fundedmonth = 1;
    SET @fundedyear = @fundedyear + 1;

 END WHILE;

END //
DELIMITER ;