I am creating a SQL script file in MySQL. The script simply creates a table and some stored procedures. I have two questions.
-
I need to check if table exists, skip the create table statement.
But if procedure exists, drop it and then create a new one. Here is
the code I am trying –CREATE TABLE IF NOT EXISTS Student ( Id INT PRIMARY KEY, Name VARCHAR(100) NOT NULL ); DROP PROCEDURE IF EXISTS usp_StudentInsert; DELIMITER $$ CREATE PROCEDURE usp_StudentInsert( id INT, name VARCHAR(100)) BEGIN INSERT INTO Student(Id, Name) VALUES(id, name); END$$ DROP PROCEDURE IF EXISTS usp_StudentRetrieve; DELIMITER $$ CREATE PROCEDURE usp_StudentRetrieve(studentId INT) BEGIN SELECT * FROM Student WHERE Id = studentId; END$$There is an error “SQL Syntax Error near DROP PROCEDURE IF EXISTS
usp_StudentRetrieve”; -
What is best way to execute script file using PHP code?
Thank you in advanced.
Ritesh
PS – I am using MySQL Workbench V5.2 with MySQL 5.1, if it helps.
I added a space after delimiter and it works for me:
from
delimiter$$todelimiter $$EDIT: