MySQL stored procedures use table names as parameters
Original article, chszs All Rights Reserved!
To forward data, contact chszs!
The scam will be prosecuted!
MySQL stored procedure uses the table name as an example to dynamically create a table with parameters.
A student asked me how to use the table name as a parameter to dynamically create a table in the MySQL stored procedure.
This problem was very troublesome before MySQL 5.0, but after MySQL 5.0.13, everything became simple because of the introduction of the prepare statement.
This issue is generally incorrect for posts searched online!
Example (verified ):
Drop procedure if exists 'newtable'; <br/> Create procedure 'newtable' (in tname varchar (64 )) <br/> begin <br/> set @ sqlcmd = Concat ('create table', tname, '(ID int not null auto_increment, name varchar (64) default null, primary Key ('id') '); <br/> prepare stmt from @ sqlcmd; <br/> execute stmt; <br/> deallocate prepare stmt; <br/> end; <br/> call newtable ('abc ');