I have a store procedure in Mysql, in this I do some query to get a string @str = 5*2+1 then I want to calculate this string and return a number.
Like in sql server I can do exec ('select'+ @str) and it returns 11
Thanks
PREPARE stmt1 FROM 'SELECT 5*2+1 as eval';
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
SELECT?EXEC. And also maybe because the OP is just simplifying an example.SELECT query directly. This seems like an attempt to avoid making a permanent stored procedure and instead making a generic facility to evaluate arbitrary SQL instead, something that always ends in tears.
You can use the following, but be careful with SQL injection when using the dynamic queries:
prepare stmt1 from 'select 5*2+1';
execute stmt1;
deallocate prepare stmt1;
Using your code example, try this (be careful with SQL injection!)
set @Calc = concat('select ', '5*2+1', ' as result');
prepare stmt1 from @Calc;
execute stmt1;
deallocate prepare stmt1;
Yes, technically you can do this, but it is a dangerous form of SQL injection, so it's probably a very bad idea.
There's a reason people do not use eval or anything like it to do simple math.
SELECTpart. It's not clear what your goal is here. Where is the5*2+1coming from?@stryou mean what? In a stored procedure?