Search code examples
c#mysqlstored-procedurestemp-tables

How can i insert data into temporary table by using a dynamic query in MySql


I want to Insert a dynamic query's result into a temporary table. How can i possible this. Plz help me...

Here is the method i tried.

CREATE TEMPORARY TABLE temp_Table
(
    Id INT
);


SET @str = 'SELECT id FROM animals';

PREPARE statement1  FROM @str;


INSERT INTO temp_Table  EXECUTE statement1;

This is not the actual Query(i mean "SELECT id FROM animals"), the Actual contain many Conditions

Thanks....


Solution

  • You can do it in one statement:

    CREATE TEMPORARY TABLE temp_Table SELECT id FROM animals;
    

    See here: http://dev.mysql.com/doc/refman/5.1/en/create-table.html