5

I want to insert the results from a dynamic query into a temp table or a way to use this result as a table. for example :

declare str varchar(2000);
set @str="select 
       from
        `cdr` ";
 PREPARE stmt1 FROM @str;
  EXECUTE stmt1;

and now i want to execute a query like this

 select * from stmt1;
0

1 Answer 1

4

EDIT: Try below Code:

DECLARE str VARCHAR(2000);
SET @str="CREATE TEMPORARY TABLE tempTable1 select * from `cdr` ";
PREPARE stmt1 FROM @str;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;

SELECT * FROM tempTable1;
-- Code what you want to work on temptable1 
DROP TABLE tempTable1;

Orginal Answer: First Create Temp Table and then use below code:

DECLARE str VARCHAR(2000);
SET @str="insert into tempTable select * from `cdr` ";
PREPARE stmt1 FROM @str;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;

Then you can execute query like this:

SELECT * FROM tempTable;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.