数据库
首页 > 数据库> > 在mysql中使用concat()准备statemnt给出错误

在mysql中使用concat()准备statemnt给出错误

作者:互联网

下面给出的代码是存储过程的一部分(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中是不可能的.
请帮忙.谢谢.

解决方法:

请试试这个.

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;

标签:mysql-error-1064,mysql,insert
来源: https://codeday.me/bug/20190826/1727799.html