0

I am new to mysql function and store procedure.Here is my function of mysql with function name create_role that takes argument role_name.What I have to do is If there is any row with with particular role name then it should return 1 or true, if there is no role then it should return 0 or false.

CREATE FUNCTION create_role ( role_name VARCHAR(100))
RETURNS INT
BEGIN
    DECLARE check_role INT;
    SET check_role = 0;
    SELECT * from role where role.role=role_name into check_role;
    RETURN check_role;
END
1
  • You have to use delimiter // at the beginning and // at the end of the function definition. Commented Apr 3, 2018 at 5:33

2 Answers 2

1

You could use IF and EXISTS:

CREATE FUNCTION create_role ( role_name VARCHAR(100))
RETURNS INT
BEGIN
    IF EXISTS (SELECT * from role where role.role=role_name) THEN
       RETURN 1;
    ELSE 
       RETURN 0;
    END IF;
END
Sign up to request clarification or add additional context in comments.

Comments

0

How about

SET check_role = (SELECT count(*) > 0 from role where role = role_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.