5

我想将动态查询的结果插入临时表或将此结果用作表的方法。例如 :

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

现在我想执行这样的查询

 select * from stmt1;
4

1 回答 1

4

编辑:试试下面的代码:

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;

原始答案:首先创建临时表,然后使用以下代码:

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

然后你可以像这样执行查询:

SELECT * FROM tempTable;
于 2012-12-13T05:04:20.297 回答