Vince Osana Vince Osana - 4 months ago 14
SQL Question

MySQL get result of stmt result for dynamic query

I am trying to get the result of the dynamic query to use in my IF condition, but I always get this error message

Error Code: 1210 Incorrect arguments to EXECUTE


What I want to happen is if the content of the column is empty, it will not add 100 to the totalamt column.

help me improve or solve my code if you have other solution or way for this problem.

Here is my code:

set total = 20;
set x = 0;
set counter = 1;
WHILE (x < total) DO

SET @cGuest = (CONCAT('name', counter));
SET @nGuest = CONCAT('SELECT ',@cGuest,' FROM guest WHERE ',iEmpID,' = idemp into @c1;');


PREPARE stmt FROM @nGuest;
SET @c1 = c2;
EXECUTE stmt USING @c1; -- USING @c1;
SELECT @c1 as result;
DEALLOCATE PREPARE stmt;

IF @c1 != '' or ' ' or NULL THEN

UPDATE employee
SET
totalamt = totalamt + 100
WHERE
iEmpID = idemp;


set x = x + 1;
set counter = counter + 1;


ELSE SELECT 'ERROR';

END IF;
END WHILE;

Answer

I believe you have an incorrect usage of SELECT...INTO

Please try this:

SET @nGuest = CONCAT('SELECT ',@cGuest,' into @c1 FROM guest WHERE ',iEmpID,' = idemp;');
Comments