In my Stored Procedure i’m passing column names and values as a text, substring them using substring_index and i’m trying to assign them like in the below code. But MySQL prompts me a syntax error.
How can I resolve this?
var1, var2 varchar(50);
set @var1 = substring_index(substring_index(substring_index(query,',',1),',',1),'=',1);
set @var2 = substring_index(substring_index(substring_index(query,',',1),',',1),'=',-1);
gives an syntax error here
update device
set @var1=@var2
where dv_id=deviceId;
Error Code : 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 '@var1=@var2
where dv_id=deviceId;
-- prepare stmt1 from @s;
-- execute stmt1 usi' at line 10
Your problem is that this:
isn’t a value SET clause for an UPDATE. MySQL won’t evaluate your variables to get at the expressions they contain. However, you can use PREPARE and EXECUTE to build and execute the SQL dynamically. Something like this: