Search code examples
mysqlsql-serverdynamic-sqlsp-executesql

Is there an procedure in MySQL similar to sp_executesql in SQL Server?


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?


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