1

So I have this stored procedure, that I have tried to write a few different way, all to no avail.

CREATE PROCEDURE `CreateHero`
(IN USER_EMAIL VARCHAR(40), IN NAME VARCHAR(16), IN GENDER VARCHAR(6))
BEGIN
    INSERT INTO HEROES (USER_ID, NAME, GENDER) 
VALUES 
((CALL GetUserId(USER_EMAIL)), NAME, GENDER);
END

I am getting this error

#1064 - 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 'CALL GetUserId(USER_EMAIL)), NAME, GENDER)' at line 6

I have tried tinkering with for a while.

GetUserId works. I tried to store the result in a temporary variable and then insert it but that did not work.

Not to be shameless but If you can determine a solution, a solution where the CALL GetUserId is stored in a variable would be best.

1
  • Is GetUserId a function or a stored procedure? Commented Sep 22, 2013 at 7:21

2 Answers 2

3

You can't use it like this. Rewrite your GetUserId procedure with an OUT parameter.

Something like this:

DELIMITER $$
CREATE PROCEDURE GetUserId(IN p_email varchar(20), OUT p_id int)
BEGIN
    SELECT id INTO p_id FROM users where email = p_email;
    /*or whatever your procedure does*/
END$$
DELIMITER ;

Then your procedure CreateHero would look like this:

DELIMITER $$
CREATE PROCEDURE `CreateHero`
(IN USER_EMAIL VARCHAR(40), IN NAME VARCHAR(16), IN GENDER VARCHAR(6))
BEGIN
    DECLARE v_id int;
    CALL GetUserId(USER_EMAIL, v_id);
    INSERT INTO HEROES (USER_ID, NAME, GENDER) 
    VALUES 
    (v_id, NAME, GENDER);
END$$
DELIMITER ;
Sign up to request clarification or add additional context in comments.

7 Comments

I dropped the old procedure, now this one is giving me issues CREATE PROCEDURE GetUserId(IN USER_EMAIL VARCHAR(40), OUT USER_ID INT) BEGIN SELECT ID INTO USER_ID FROM USERS WHERE EMAIL=USER_EMAIL; END
No, don't use the same names for variables and columns.
I am not sure what you mean, yo
The procedure is just an example, I hope you haven't used it 1:1. And you have replaced my parameter names starting with p_ with USER_, which are the column names of your heroes table. Don't do that, it'll confuse MySQL. Have you used appropriate delimiter? What's the error message?
I changed it to p_ and get the error #1064 - 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 3
|
1

Old thread but could help some one looking for later... Base on fancyPants answer but more beautiful like this:

 DELIMITER $$
    CREATE FUNCTION GetUserId(IN p_email varchar(20)) RETURNS int
    BEGIN
        RETURN(SELECT id FROM users where email = p_email);
        /*or whatever your function does*/
    END$$
    DELIMITER ;

And then:

DELIMITER $$
CREATE PROCEDURE `CreateHero` (IN USER_EMAIL VARCHAR(40),
    IN NAME VARCHAR(16), IN GENDER VARCHAR(6))
BEGIN
    INSERT INTO HEROES (USER_ID, NAME, GENDER) 
        VALUES (GetUserId(USER_EMAIL), NAME, GENDER);
END$$
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.