0

I am writing a stored procedure in phpmyadmin but I am getting a syntax error near Update statement. But if I put my update statement inside my if statement than all errors been resolved.

Q: Why update statement outside causes an error?

CREATE DEFINER=`root`@`localhost` PROCEDURE `tbl_people_update`(IN `p_Id` INT, IN `p_Name` VARCHAR(150), IN `p_Mobile_Number` VARCHAR(100))
    IF CHAR_LENGTH(p_Mobile_Number) <= 0 THEN
     SET p_Mobile_Number = '12345';
    END IF;

    UPDATE tbl_people
     SET 
     Name=p_Name,
     Mobile_Number = p_Mobile_Number
     WHERE
     Id=p_Id;

The error is as follows:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UPDATE tbl_people SET Name=p_Name, Mobile_Number = p_Mobile_Number WHER' at line 5

0

2 Answers 2

1
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `tbl_people_update`(IN `p_Id` INT, IN `p_Name` VARCHAR(150), IN `p_Mobile_Number` VARCHAR(100))
BEGIN
     IF CHAR_LENGTH(p_Mobile_Number) <= 0 THEN
     SET p_Mobile_Number = '12345';
    END IF;
    UPDATE tbl_people
     SET 
     Name=p_Name,
     Mobile_Number = p_Mobile_Number
     WHERE
     Id=p_Id;
END$$
DELIMITER ;

Try above code.

Hope this will helps you.

Sign up to request clarification or add additional context in comments.

Comments

1

You've forgotten re-define delimiter, try this:

delimiter ||
CREATE DEFINER=`root`@`localhost` PROCEDURE `tbl_people_update`(IN `p_Id` INT, IN `p_Name` VARCHAR(150), IN `p_Mobile_Number` VARCHAR(100))
    IF CHAR_LENGTH(p_Mobile_Number) <= 0 THEN
     SET p_Mobile_Number = '12345';
    END IF;

    UPDATE tbl_people
     SET 
     Name=p_Name,
     Mobile_Number = p_Mobile_Number
     WHERE
     Id=p_Id;
||
delimiter ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.