0

我在 MySQL 5.6.11 中运行查询。我创建了以下存储过程

DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `digital_audio_test_detail`(IN temp VARCHAR(50), IN temp2 VARCHAR(50))
BEGIN
    SET @temp_query = CONCAT('SELECT * FROM ',temp);
    SET @final_query = CONCAT(@temp_query,'WHERE', temp,'.unit_test_result_id =',temp2);
    PREPARE stmt FROM @final_query;
    EXECUTE stmt;
END

当我在查询页面中调用此过程时,在其调用时出现语法错误。这就是我执行查询的方式。

SELECT unit_test_result.*, @temp1 := unit_test.name, @temp2 := unit_test_result.id 
FROM unit_test_result, unit_test 
WHERE unit_test_result.test_run_id = 2 
  AND unit_test.id = unit_test_result.unit_test_id;
CALL digital_audio_test_detail(@temp1, @temp2);

我必须将两个参数传递给该过程。当我创建一个只有第一个参数的过程并调用它一个参数时,它执行得很好。但是当我使用两个参数时,我得到语法错误。需要帮忙。谢谢

4

1 回答 1

2
SET @final_query = CONCAT(@temp_query,'WHERE', temp,'.unit_test_result_id =',temp2);

您正在构建的字符串不是有效的查询。

在 WHERE 前后添加空格,如

SET @final_query = CONCAT(@temp_query,' WHERE ', temp,'.unit_test_result_id =',temp2);
于 2013-06-19T19:07:52.687 回答