3

下面给出的代码是存储过程的一部分(mysql 5.5.20):

prepare stmnt1 from concat('insert into ' , tableName ,  '(employee_id , administrator_id) values(? , ?) ' ) ;                   
set @a = 19 ;
set @b = 11;
execute stmnt1 using @a , @b ;

tableName是局部变量

当我创建程序时,我收到此错误:

#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 'concat('insert into ' , 
tableName ,  '(employee_id , administrator_id) values(? ' at line 23

好像这种说法在mysql中是不可能的。
请帮忙。谢谢。

4

2 回答 2

5

请试试这个。

set @sql = concat('insert into ' , tableName ,  '(employee_id , administrator_id) values(? , ?) ' ) ; 
prepare stmnt1 from @sql;                
set @a = 19 ;
set @b = 11;
execute stmnt1 using @a , @b ;

deallocate prepare stmnt1;
于 2012-07-20T13:18:49.487 回答
4

在变量中使用它

set @sql_string = 
concat('insert into ' , tableName ,  '(employee_id , administrator_id) values(? , ?) ' ) ;  
prepare stmnt1 from @sql_string;                 
set @a = 19 ; 
set @b = 11; 
execute stmnt1 using @a , @b ; 
于 2012-07-20T13:21:26.237 回答