I am converting a MSSQL Stored Procedure to MySQL, and am running into a problem. Maybe it's because I have been staring at the code for so long, but I can't seem to figure it out:
CREATE PROCEDURE usp_AddNewUser
(IN UserName VARCHAR(50),
IN UserID VARCHAR(50),
IN Password VARCHAR(30),
IN PageAccess VARCHAR(10),
IN AdminUser VARCHAR(50))
BEGIN
Declare RowID, CountAll INT DEFAULT 0
SELECT @CountAll := COUNT(*) FROM WebUsers WHERE RTRIM(UserID) = RTRIM(@UserID);
IF @CountAll = 0 THEN
INSERT INTO WebUsers (UserName, UserID, UserPassword, AddedBy, UpdatedBy) Values (UserName, UserID, Password, AdminUser, AdminUser);
SELECT @RowID := @@Identity;
ELSE
SELECT @RowID := RowID FROM WebUsers WHERE RTRIM(UserID) = RTRIM(@UserID);
UPDATE WebUsers SET UpdatedBy = @AdminUser, UpdatedOn = NOW() WHERE RowID = @RowID;
END IF;
If @PageAccess Like ('%4%') THEN
INSERT INTO WebUserTypeMapping (WebUserRowID, WebUserTypeRowID) Values (@RowID, 4);
ELSE
If @PageAccess Like ('%1%') THEN
INSERT INTO WebUserTypeMapping (WebUserRowID, WebUserTypeRowID) Values (@RowID, 1);
END IF;
END IF;
END;
The syntax error is at the SELECT@CountAll line:
SELECT @CountAll := COUNT(*) FROM WebUsers WHERE RTRIM(UserID) = RTRIM(@UserID)
I've tried rewriting it several times, but no dice. There are likely additional errors in this code too that I don't know about yet since I can't get by that error.
Can anyone help me out? Not that it matters, but it would totally make my Friday… 🙂
Thanks!
Edit: The full error message is the following:
#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 'SELECT @CountAll := COUNT(*) FROM WebUsers WHERE RTRIM(UserID) = RTRIM(@UserID)' at line 12
MySQL Server version: 5.5.30
Edit: When setting a different delimiter, I get an error message that literally just says "Error" — with no other information.
Best Answer
Comment from @ypercube is correct -- in this case, the syntax error you're encountering is because the previous line did not end in a semicolon:
Should be:
In MySQL's stored procedure language, you have access to two different types of variables.
The first you declare with
DECLARE
and these are locally scoped to the procedure. Do not prefix these with@
.The second type of variable are called user variables and they are prefixed with
@
. You don't have to declare these. But they are kind of like "global" variables in that they are scoped to the session, and you can assign and read them outside the procedure. That is, you could assign a value to one of these variables in the procedure, and then after the procedure is done, you could read that user variable in any SQL expression.In your procedure, you also have some input arguments. These are treated like the first type of variable. They are locally scoped, and they don't use the
@
prefix. For example, you have an input argumentUserID
but this is not the same variable as@UserID
that you use in your query.This brings up the possibility of ambiguity when your variables have the same names as columns, and you try to use the variables to compare to the columns. MySQL does its best to have rules for precedence, but it is far easier to debug if you make sure your local variable names don't use the same names as columns.
Other issues:
This line:
Should be as follows in MySQL:
Comments starting with
--
must have a space following them, which is non-ANSI but is meant to resolve syntax ambiguity. E.g., this:Must be changed to: