MySQL动态查询中的语法错误

时间:2015-02-16 11:08:55

标签: mysql

我很长一段时间以来一直在MySQL中遇到这个问题。我的存储过程中有一个动态查询,

delimiter //

CREATE PROCEDURE test_proc ()
BEGIN

DECLARE var INT DEFAULT 1;
DECLARE id VARCHAR(500) DEFAULT '0';

WHILE  var <= 10
DO

SET id = CONCAT(id ,',',(SELECT CAST(id AS CHAR) FROM test_client WHERE Rank = var));
SET var = var + 1;

END WHILE;

SET SQL_QUERY = CONCAT('
         CREATE TEMPORARY TABLE t1
         AS
         SELECT *
         FROM test WHERE id IN (',id,');');

SET SQL_QUERY1 = CONCAT('     
         CREATE TEMPORARY TABLE t2
         AS
         SELECT *
         FROM test1 WHERE id IN (',id,')');

SET @SWV_Stmt = CONCAT(SQL_QUERY,SQL_QUERY1);
PREPARE SWT_Stmt FROM @SWV_Stmt;
EXECUTE SWT_Stmt;
DEALLOCATE PREPARE SWT_Stmt;

END //

delimiter ;

当我创建并执行此操作时,我得到以下错误,

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TEMPORARY TABLE t2 AS SELECT *' at line 5

有人可以为此提供理由和解决方案吗?

提前致谢...

1 个答案:

答案 0 :(得分:2)

这不是一个单一的陈述,即两个。 documentation states

  

文本必须代表单个陈述,而不是多个陈述。