How to resolve the MySQL 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?”

MySQLMySQLi Database

To avoid this type of error in MySQL stored procedure, you need to change the delimiter ; to //.

Suppose if you are using stored procedure or triggers or even function then you need to change the delimiter. The syntax is as follows.

DELIMITER //
   CREATE PROCEDURE yourProcedureName()
   BEGIN
      Statement1,
      .
      .
   N
END;
//
DELIMITER ;

To understand the above syntax, let us create a stored procedure. The query to create a stored procedure is as follows -

mysql> DELIMITER //
mysql> CREATE PROCEDURE sp_getAllRecords()
-> BEGIN
-> SELECT *FROM employeetable;
-> END;
-> //
Query OK, 0 rows affected (0.23 sec)
mysql> DELIMITER ;

Call the stored procedure using CALL command. The syntax is as follows.

CALL yourStoredProcedureName();

Now call the above procedure that returns all records of Employee table. The query is as follows.

mysql> CALL sp_getAllRecords();

The following is the output.

+------------+--------------+----------------+
| EmployeeId | EmployeeName | EmployeeSalary |
+------------+--------------+----------------+
| 2 | Bob | 1000 |
| 3 | Carol | 2500 |
+------------+--------------+----------------+
2 rows in set (0.00 sec)
Query OK, 0 rows affected (0.02 sec)
raja
Published on 11-Jan-2019 10:46:18
Advertisements