Currently, I am experimenting with some JavaScript code in Node.js, utilizing the MySQL package within Node and running MySQL queries in the Visual Studio Code terminal.
Despite trying various approaches such as adding (), '', "", etc., isolating the update and insert statements individually, and executing them against a table in PHPMyAdmin—both statements perform correctly. The issue arises when incorporating "IF @@ROWCOUNT=0" into the statement, causing the entire query to fail.
The script executes the following SQL statement stored in a JavaScript variable:
var sql = "UPDATE players SET userID='123',username='FNNewUser',site='blah' WHERE userID='123' IF @@ROWCOUNT=0 PRINT 'NO ROWS UPDATED'";
Upon executing the script, it initially appears to be successful but subsequently throws an error message:
Error: ER_PARSE_ERROR: 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 'IF @@ROWCOUNT=0 PRINT 'NO ROWS UPDATED'' at line 1
at Query.Sequence._packetToError (C:\node\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
...
The primary objective is to verify if a record already exists in MySQL; if not, proceed with the insert statement. While this query might have been addressed on StackOverflow, I am intrigued by why introducing the IF conditional causes the SQL statement to fail.