0

I'm trying to write a MySQL function which returns whether an username is in my table or not. It's the following:

CREATE FUNCTION UserExists (pUserName VARCHAR(40))
RETURNS BIT DETERMINISTIC

BEGIN

DECLARE rVal BIT;

IF EXISTS (SELECT * FROM Users WHERE userName = pUserName)
THEN SET rVal = 1;
ELSE SET rVal = 0;
END IF;

RETURN rVal;

END;

However, I get an error. Any ideas?

1
  • 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 6. Commented Feb 2, 2013 at 17:11

3 Answers 3

2

try this

    IF(EXISTS(SELECT * FROM Users WHERE userName = pUserName))
Sign up to request clarification or add additional context in comments.

Comments

1

how about using user define variable?

DELIMITER $$

CREATE FUNCTION UserExists (pUserName VARCHAR(40))
RETURNS BIT DETERMINISTIC
BEGIN

    DECLARE rVal BIT;
    SET @recCount := (SELECT COUNT(*) FROM Users WHERE userName = pUserName);
    IF @recCount > 0 THEN 
        SET rVal = 1;
    ELSE 
        SET rVal = 0;
    END IF;
    RETURN rVal;

END $$

DELIMITER ;

3 Comments

Thanks for your reply. However, didn't work either. Error: #1419 - You do not have the SUPER privilege and binary logging is enabled (you might want to use the less safe log_bin_trust_function_creators variable)
the error is not the syntax but because maybe you don't have enough privilege to create a function.
I have a shared godaddy hosting account. Any idea to solve this issue?
0

Before you query your mysql database, you need to use %mysql sudo -u -p and then give your commands.

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.