In a MYSQL Stored-Procedure I need to select both the table columns, and select columns into variables for further use within that procedure.
Below, a simplified version of the stored-procedure.
DECLARE pUSR_ID BIGINT;
DECLARE pPRJ_ID BIGINT;
DECLARE pUSR_LAN_ID BIGINT;
SELECT USR_ID, USR_Login, USR_Password, USR_Archived, USR_Deleted,
USR_ID, USR_PRJ_ID, USR_LAN_ID INTO pUSR_ID, pPRJ_ID, pUSR_LAN_ID
FROM tblUsers
WHERE USR_Login = 'foobar';
When I execute the above stored-procedure, I get the following error message, which makes sense.
Error Code: 1222. The used SELECT statements have a different number of columns
So, I placed the variables first, and the other columns after the variables.
SELECT USR_ID, USR_PRJ_ID, USR_LAN_ID INTO pUSR_ID, pPRJ_ID, pUSR_LAN_ID,
USR_ID, USR_Login, USR_Password, USR_FullName, USR_IsProjectAdmin, USR_Archived,
USR_Deleted
Executing this returns: Error 1327: Undeclared variable: USR_ID. I understand why I am getting the error.
What I don't understand is how I can both select the table columns and select certain columns into variables in the same statement?
1 Answer