0

I have a stored procedure which is syntactically acceptable on my local PC, where I have MySQL workbench 5.2.44 and I think my local version of MySQL is 5.5.29 judging by the folder names. Here is the code, which I have reduced to a minimum for illustration

DELIMITER $$

CREATE PROCEDURE `usp_insert_booking_test_2`(
        IN  p_dtmTimeSlot DATETIME  
     )

BEGIN 

     INSERT INTO tblBookings (dtmTimeSlot)
     SELECT p_dtmTimeSlot
     FROM tblBookings
     WHERE NOT EXISTS (SELECT * FROM tblBookings WHERE dtmTimeSlot = p_dtmTimeSlot) LIMIT 1;

END$$

As you can see, I am trying to ensure I do not create two bookings in the same time slot. Yes, I have a constraint on the table to do this also. Belt & braces. When I try to add this procedure on my remote host (running MySQL version 5.1.68-cll), it says

#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 '' at line 12

Line 12 is the WHERE NOT EXISTS line. I have tried removing LIMIT 1, that makes no difference. Is there a way to write the query so that MySQL 5.1 will accept it?

Stuart M
  • 11,458
  • 6
  • 45
  • 59
DJDave
  • 865
  • 1
  • 13
  • 28
  • 1
    The Last Line should be `END $$` (Space mission) And the line missing after the last line: `DELIMITER ;` – Kovge Apr 18 '13 at 07:44
  • 1
    A space missing after `END` Same question: http://stackoverflow.com/questions/1102109/mysql-delimiter-syntax-errors – Kovge Apr 18 '13 at 07:45

2 Answers2

0

Use this.

DELIMITER $$

CREATE PROCEDURE `usp_insert_booking_test_2`(
    IN  p_dtmTimeSlot DATETIME  
 )

BEGIN 

 INSERT INTO tblBookings (dtmTimeSlot)
 SELECT p_dtmTimeSlot
 FROM tblBookings
 WHERE NOT IN (SELECT * FROM tblBookings WHERE dtmTimeSlot = p_dtmTimeSlot LIMIT 1);

END $$
DELIMITER ;

here somevalue is that you want to insert.

Bharat Chodvadiya
  • 1,644
  • 4
  • 20
  • 31
  • Neither of the suggestions work. The suggested answer breaks one SQL statement into two unrelated statements which change the semantics completely. The suggestion that a space is missing does not fix the error. – DJDave Apr 18 '13 at 10:31
0

i needed to add

DELIMITER ;

to the end of the code as shown. No idea why.

DJDave
  • 865
  • 1
  • 13
  • 28