1

I'm unable to declare a mysql variable inside a stored procedure. Also i cannot open a cursor as well i keep getting the same 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 '@userID INT; DECLARE @cursorUserID CURSOR FOR SELECT (MAX(userID) + 1) FROM Use' at line 3.

Here's the code:

use phpBank;
DELIMITER //
CREATE PROCEDURE newUser()
BEGIN
       DECLARE @userID INT;
       DECLARE @cursorUserID CURSOR FOR SELECT (MAX(userID) + 1) FROM Users;
       OPEN @newUserID;
END;
//
DELIMITER ;
2
  • Why would you possibly need a cursor to just select one row..? Commented Jan 7, 2014 at 8:23
  • Using this code i get the 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 '@userID int; DECLARE @cursorUserID AS CURSOR; Commented Jan 9, 2014 at 3:32

3 Answers 3

2
  1. As it has already been mentioned by others you're mixing local variables (without @ in front of a variable name) and user(session) variables (that have @ in front of a variable name). DECLARE is used only for local variables. Session variables are always available and don't require declaration.

  2. It a good practice not to give your variables and procedure parameters the same names as column names in your tables. It can easily blow your code in situation when MySQL can't decide what you actually meant to use a variable or a column.

  3. You definitely don't need a cursor (unlike plsql in Oracle) to return a scalar value by using an aggregate function (MAX() in your case). Just put a select in parentheses.

That being said a working syntactically correct version of your code might look like this

DELIMITER $$
CREATE PROCEDURE newUser()
BEGIN
    DECLARE newid INT;

    SET newid = (SELECT COALESCE(MAX(userID), 0) + 1 FROM Users);
    SELECT newid;
END$$
DELIMITER ;

or technically you can also can do (but not recommended) this

DELIMITER $$
CREATE PROCEDURE newUser()
BEGIN
    SET @newid = (SELECT COALESCE(MAX(userID), 0) + 1 FROM Users);
    SELECT @newid newid;
END$$
DELIMITER ;

Here is SQLFiddle demo


Now, deducing from procedure name and a query you're trying to use it look like you're reinventing a weel of generating unique ids (most likely PK) doing it manually. Stop! Don't do that. It creates racing conditions and won't work correctly in concurrent environment, because multiple processes can potentially grab the same MAX(userID) value.

The only safe method for generating unique ids in MySQL is auto_increment column. Use it instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it has helped.
0

Try this

use phpBank;

DELIMITER //
CREATE PROCEDURE newUser()

BEGIN
        DECLARE @userID int;
        DECLARE @cursorUserID AS CURSOR; 
        SET @cursorUserID = CURSOR FOR SELECT (MAX(userID) + 1) FROM Users;

        OPEN @newUserID;

END;
//
DELIMITER ;

Comments

0

Try this

DELIMITER //
    CREATE PROCEDURE newUser()
    BEGIN 
            DECLARE userID INT;
            DECLARE cursorUserID CURSOR FOR SELECT (MAX(userID) + 1) FROM Users;
            -- OPEN newUserID;

    END;
    //
    DELIMITER ;

CALL newUser();

1 Comment

Using this code i get the 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 '@userID INT; DECLARE @cursorUserID CURSOR FOR SELECT (MAX(userID) + 1) FROM Use' at line 3'

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.