1

I must be missing something simple because I can't figure out what is causing my script to fail. Below is the stored procedure I've written:

CREATE PROCEDURE `Search_contacts`(IN `in_owner_id` INT,
    IN `in_first_name` VARCHAR(255))
 IF in_first_name IS NOT NULL THEN
     SELECT * FROM `contacts`
     WHERE `owner_id` = in_owner_id AND `first_name` LIKE in_first_name;
END IF;

When I try and execute this on my MySQL server I get the following 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 '' at line 5

I'd like to know what is causing this error and why so I can avoid it again.

Any help is appreciated!

1 Answer 1

2

Try adding "BEGIN", "END" and "DELIIMITER", like this:

DELIMITER $$
CREATE PROCEDURE `Search_contacts`(IN `in_owner_id` INT,
    IN `in_first_name` VARCHAR(255))
BEGIN
    IF in_first_name IS NOT NULL THEN
        SELECT * FROM `contacts`
        WHERE `owner_id` = in_owner_id AND `first_name` LIKE in_first_name;
    END IF;
END $$
DELIMITER ;
Sign up to request clarification or add additional context in comments.

1 Comment

I had a feeling I was forgetting something simple, thank you! Is it best practice to always include "DELIMITER", "BEGIN" and "END"?

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.