0

So, I am creating a mysql EVENT that computes the sales every month from transaction_tbl. I can't find the error I made.

Here's my query:

DELIMITER |

 CREATE EVENT compute_sales
 ON SCHEDULE
      EVERY 1 MONTH STARTS '2018-10-01 22:00.00'
 DO
      BEGIN

 DECLARE mmsales DECIMAL(11, 2)
 SET mmsales = SELECT SUM(total_amount)
           FROM transaction_tbl
           WHERE MONTH(transaction_date) = MONTH(CURRENT_DATE()) AND
                YEAR(transaction_date) = YEAR(CURRENT_DATE())

 INSERT INTO sales_tbl (sales_year, sales_month, total_sales, time_frame) VALUES 
          (YEAR(CURRENT_DATE), MONTHNAME(CURRENT_DATE), mmsales, CURRENT_DATE)

 END |

 DELIMITER ;

Here's the error I'm getting:

#1064 - 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 'SET mmsales = SELECT SUM(total_amount) FROM transaction_tbl' at line 8

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
Cheryl Blossom
  • 185
  • 1
  • 13

1 Answers1

1

You need brackets:

SET mmsales = (SELECT SUM(total_amount)
               FROM transaction_tbl
               WHERE MONTH(transaction_date) = MONTH(CURRENT_DATE())
                 AND YEAR(transaction_date) = YEAR(CURRENT_DATE()));

Plus you should end each statement with semicolon.

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275