Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prepare statemnt using concat() in mysql giving error [closed]

The given below code is part of stored procedure (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 is local variable

When i create procedure i get this error :

#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

It seems that this kind of statement is not possible in mysql.
Please help.Thanks.

like image 522
Ravi Jain Avatar asked Nov 01 '25 05:11

Ravi Jain


1 Answers

Try this one please.

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;
like image 168
fancyPants Avatar answered Nov 03 '25 03:11

fancyPants



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!