So I've created a few functions who's purpose is to get the total number of tickets for each order, which may be spread about multiple rows. I am pretty sure I used BEGIN, END and DELIMITERS correctly which was the fixes of other questions on this site. However when i run this SQL query, i get this:
Error
Static analysis:
1 errors were found during analysis.
Missing expression. (near "ON" at position 25)
SQL query: Copy Edit Edit
SET FOREIGN_KEY_CHECKS = ON;
MySQL said: Documentation
#2014 - Commands out of sync; you can't run this command now
Here is my query for reference
DROP PROCEDURE IF EXISTS getTotalTicketsPerOrder;
DROP PROCEDURE IF EXISTS getTotalTicketsBoughtPerOrder;
DELIMITER //
CREATE PROCEDURE getTotalTicketsPerOrder(
IN pOrderID INT(11),
OUT pTotalTickets INT(11)
)
BEGIN
SELECT SUM(ticketsinorder.NoOfTicketsBought)
INTO pTotalTickets
FROM
ticketsinorder
WHERE
ticketsinorder.OrderID = pOrderID ;
END //
DELIMITER ;
DELIMITER //
CREATE PROCEDURE getTotalTicketsBoughtPerOrder(IN idCount INT(11))
BEGIN
DECLARE counter INT DEFAULT 1;
WHILE counter <= idCount DO
CALL getTotalTicketsPerOrder(counter, @totalTicketsBought) ;
SELECT @totalTicketsBought ;
SET counter = counter + 1 ;
END WHILE ;
END //
DELIMITER ;
SELECT @idCount := COUNT(orders.OrderID)
FROM orders;
CALL getTotalTicketsBoughtPerOrder(@idCount);
Since I have a feeling some of you will ask, here's the table in question Table Photo
EDIT: I have found the source of the problem (kinda)
So i found that the error message only sends when i include the last line ( CALL getTotalTicketsBoughtPerOrder(@idCount);
)
UPDATE: Included error logs
2022-08-22 20:10:26 0 [Note] mysqld.exe: Aria engine: recovery done
2022-08-22 20:10:26 0 [Note] InnoDB: Mutexes and rw_locks use Windows interlocked functions
2022-08-22 20:10:26 0 [Note] InnoDB: Uses event mutexes
2022-08-22 20:10:26 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2022-08-22 20:10:26 0 [Note] InnoDB: Number of pools: 1
2022-08-22 20:10:26 0 [Note] InnoDB: Using SSE2 crc32 instructions
2022-08-22 20:10:26 0 [Note] InnoDB: Initializing buffer pool, total size = 16M, instances = 1, chunk size = 16M
2022-08-22 20:10:26 0 [Note] InnoDB: Completed initialization of buffer pool
2022-08-22 20:10:26 0 [Note] InnoDB: Starting crash recovery from checkpoint LSN=2084794
2022-08-22 20:10:26 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
2022-08-22 20:10:26 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
2022-08-22 20:10:26 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2022-08-22 20:10:26 0 [Note] InnoDB: Setting file 'C:\xampp\mysql\data\ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2022-08-22 20:10:26 0 [Note] InnoDB: File 'C:\xampp\mysql\data\ibtmp1' size is now 12 MB.
2022-08-22 20:10:26 0 [Note] InnoDB: Waiting for purge to start
2022-08-22 20:10:26 0 [Note] InnoDB: 10.4.22 started; log sequence number 2084803; transaction id 4233
2022-08-22 20:10:26 0 [Note] InnoDB: Loading buffer pool(s) from C:\xampp\mysql\data\ib_buffer_pool
2022-08-22 20:10:26 0 [Note] Plugin 'FEEDBACK' is disabled.
2022-08-22 20:10:26 0 [Note] InnoDB: Buffer pool(s) load completed at 220822 20:10:26
2022-08-22 20:10:26 0 [Note] Server socket created on IP: '::'.
UPDATE 2: Specific location of error found.
So i did a little digging and realised the cause of the problem was the select statement within the while loop. SELECT @totalTicketsBought ;
I'm not sure why that is a problem but it indeed is.