Question

I would like to make dynamic queries in within my procedure. SQL Server has neat sp_executesql procedure for such tasks, is there anything in MySQL which can help me to achieve similar functionality?

Was it helpful?

Solution

AFAIK there is nothing exactly same. However, you can use a prepared statement, like:

mysql> PREPARE stmt1 FROM 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';
mysql> SET @a = 3;
mysql> SET @b = 4;
mysql> EXECUTE stmt1 USING @a, @b;
+------------+
| hypotenuse |
+------------+
|          5 |
+------------+
mysql> DEALLOCATE PREPARE stmt1;

Copied from here: http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html

OTHER TIPS

You will probably have to use prepared statements for that purpose using MySQL:

SET @query = 'INSERT INTO tbl (a) VALUES (?)';
PREPARE stmt FROM @query;

...
SET @param = 'hello';
EXECUTE stmt USING @param;
...

DEALLOCATE PREPARE stmt;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top