1

I want to create a simple function like below to do an easy task:

DELIMITER $$
CREATE FUNCTION f(key TEXT, str TEXT) RETURNS INT
BEGIN
    IF LOCATE(key, str) > 0 THEN
        RETURN 1;
    ELSE
        RETURN 0;
    END IF;
END $$
DELIMITER ;

But I got an error:

ERROR 1064 (42000): 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 'key T
EXT, str TEXT) RETURNS INT
BEGIN
    IF LOCATE(key, str) > 0 THEN
        R' at line 1

What's the problem?

2
  • 2
    What's the error? Please think about how people might go about diagnosing this problem before submitting your question. Commented Jan 28, 2013 at 18:38
  • 1
    I have a feeling that key is blue. Commented Jan 28, 2013 at 18:39

2 Answers 2

3

The function has a syntax error because key is a reserved word in MySql. Solution: rename the parameter or put key inside backticks like this:

CREATE FUNCTION f(`key` TEXT, str TEXT) RETURNS INT
Sign up to request clarification or add additional context in comments.

Comments

1

key is a keyword in mysql, put it between ` or use different name.

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.