views:

40

answers:

3

I use the following mysql query,

DELIMITER $$
DROP PROCEDURE IF EXISTS `allied`.`aboutus_delete`$$
CREATE DEFINER=`allied`@`%` PROCEDURE `aboutus_delete`(
IN p_Id int(11)
)
BEGIN
   if exists(   select aboutUsId 
                  from aboutus 
                 where aboutUsId=p_id 
                   and isDeleted=0
            )
      update aboutus set isDeleted=1 where aboutUsId=p_id
   else
      select 'No record to delete'
END$$
DELIMITER ;

But i get this error when i execute it...

Error Code : 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 
'update aboutus set isDeleted=1 where aboutUsId=p_id
else
   select 'No record to' at line 6

EDIT:

using semicolon doesn't seem to work,

if exists(select aboutUsId from aboutus where aboutUsId=p_id and 
 isDeleted=0) then
   update aboutus set isDeleted=1 where aboutUsId=p_id;
else
   select 'No record to delete';
A: 

You missed the 'THEN' in 'IF'...

sza
@ziang it is an if exists statement..
bala3569
@ziang adding then doesnt work...
bala3569
You also need the end if
sza
+2  A: 

This is a different issue: you can optimize this procedure a bit. Why hit the datastore twice when one query will do? Just set the isDeleted attribute to 1 and check the row_count value afterwards.

BEGIN
  UPDATE aboutus SET isDeleted = 1 WHERE aboutUsId = p_id AND isDeleted = 0;
  IF (SELECT row_count()) <= 0 THEN
    SELECT 'No record to delete';
  END IF;
END
Barry Brown
A: 

Along with the semicolons and THEN, you are missing END IF to terminate the IF statement.

Barry Brown