mysqlmysql-function

ERROR 1064 (42000): SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ');


I am trying to write a mysql function... but I am getting the syntax error as

ERROR 1064 (42000): 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 ');

My code is like below,

delimiter //
CREATE FUNCTION calculateNetItemStandardDiscountAmount(productId INT,quantity INT) RETURNS DOUBLE 
BEGIN
   DECLARE discountPer DOUBLE;
   DECLARE unitPrice DOUBLE;
   DECLARE netItemDiscountAmount DOUBLE;
   DECLARE netItemAmount DOUBLE;

   SELECT `SellingUnitPrice` into unitPrice 
   FROM `TBL_PRODUCT_MASTER` 
   WHERE `Id` = productId;
   SET netItemAmount = quantity * unitPrice ;
   insert into debugtable select concat('ACTUAL AMOUNT ', netItemAmount);

   SELECT discDetail.`DiscountPercentage` into discountPer 
   FROM `TBL_DISCOUNT_DETAIL` AS discDetail , `TBL_DISCOUNT` AS disc, TBL_PRODUCT_DISCOUNT AS prodDisc 
   WHERE disc.`DiscountStartDate` < NOW() 
         AND disc.`DiscountEndDate` > NOW() 
         AND disc.`SchemeType` = 'AMOUNT' 
         AND discDetail.DiscountId = disc.Id 
         AND prodDisc.productId = productId 
         AND prodDisc.discountId = disc.Id 
         AND prodDisc.`IsEnabled` = 1 
         AND disc.`IsEnabled` = 1 
         AND (quantity * unitPrice) >= discDetail.LowerLimit and (quantity * unitPrice) <= discDetail.HigherLimit);

   insert into debugtable select concat('STD DISCOUNT PER BY AMOUNT ', discountPer);
   IF (discountPer IS NOT NULL) THEN
       SET netItemDiscountAmount = ((netItemAmount) * (discountPer * 0.01));
       insert into debugtable select concat('APPLY VOLUME DISCOUNT ', netItemDiscountAmount);
   END IF;

   SELECT discDetail.`DiscountPercentage` into discountPer 
   FROM `TBL_DISCOUNT_DETAIL` AS discDetail , `TBL_DISCOUNT` AS disc, TBL_PRODUCT_DISCOUNT AS prodDisc 
   WHERE disc.`DiscountStartDate` < NOW() 
         AND disc.`DiscountEndDate` > NOW() 
         AND disc.`SchemeType` = 'VOLUME' 
         AND discDetail.DiscountId = disc.Id AND prodDisc.productId = productId 
         AND prodDisc.discountId = disc.Id 
         AND prodDisc.`IsEnabled` = 1  
         AND disc.`IsEnabled` = 1 
         AND (Quantity >= discDetail.LowerLimit and Quantity <= discDetail.HigherLimit);

   insert into debugtable select concat('STD DISCOUNT PER BY VOLUME ', discountPer);
   IF (discountPer IS NOT NULL) THEN
       SET netItemDiscountAmount = netItemDiscountAmount + ((netItemAmount) * (discountPer * 0.01));
       insert into debugtable select concat('APPLY AMOUNT DISCOUNT ', netItemDiscountAmount);
   END IF;

   RETURN netItemDiscountAmount;
END//
delimiter ;

Any help would be highly appreciated... Thanks in advance...


Solution

  • Looks like you have unopened brackets at the end of this line

    SELECT discDetail.`DiscountPercentage` into discountPer FROM `TBL_DISCOUNT_DETAIL` AS discDetail , `TBL_DISCOUNT` AS disc, TBL_PRODUCT_DISCOUNT AS prodDisc WHERE disc.`DiscountStartDate` < NOW() AND disc.`DiscountEndDate` > NOW() AND disc.`SchemeType` = 'AMOUNT' AND discDetail.DiscountId = disc.Id AND prodDisc.productId = productId AND prodDisc.discountId = disc.Id AND prodDisc.`IsEnabled` = 1 AND disc.`IsEnabled` = 1 AND (quantity * unitPrice) >= discDetail.LowerLimit and (quantity * unitPrice) <= discDetail.HigherLimit)