0
SET Project_List_val=CONCAT(Project_Number_val,'_List');
Insert Into test (Manthan_Panel_Id) select Manthan_Panel_Id from Project_List_val where Project_Number_val='9';

在插入语句中有一个名为“Project_List_val”的变量,它由上述步骤中连接的表名组成。该语句没有将变量的内容作为表名,而是将“Project_List_val”作为表名并给出未找到表的错误。

有什么建议么?

4

1 回答 1

3

默认情况下,您不能参数化表名和列名,因此您需要为此创建动态 SQL

SET @Project_List_val = CONCAT(Project_Number_val, '_List');
SET @projNum = 9;
SET @sql = CONCAT(' INSERT INTO test (Manthan_Panel_Id)
                    SELECT Manthan_Panel_Id 
                    FROM ', @Project_List_val, ' 
                    WHERE Project_Number_val = ?');

PREPARE stmt FROM @sql;
EXECUTE stmt USING @projNum;
DEALLOCATE PREPARE stmt;
于 2013-02-12T09:02:42.487 回答