Home > database >  Mysql build table through stored procedures, including field, annotation, etc
Mysql build table through stored procedures, including field, annotation, etc

Time:09-23

Create procedure sp_create_tab ()
The begin
The set @ sql_t=concat (" the create table... ");
Prepare sql_t from @ sql_t;
The execute sql_t;
End
Need dynamic passing parameters (table name, field name, attribute)
Stored procedure how to write here, thank you

CodePudding user response:

DROP PROCEDURE if the EXISTS proc_create_table;
CREATE PROCEDURE proc_create_table (IN table_name VARCHAR (32))
The BEGIN
DECLARE tblname VARCHAR (32);
DECLARE sql1 VARCHAR (1000);
The SET tblname=CONCAT (table_name);
The set @ sql1=CONCAT (" CREATE TABLE ` ", tblname, "` (` id ` int (11), NOT NULL AUTO_INCREMENT, ` username ` varchar (255) the DEFAULT NULL, ` password ` varchar (255) the DEFAULT NULL, PRIMARY KEY (` id `)) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8; ");
# the select sql1;
PREPARE STMT from @ sql1;
The EXECUTE STMT.
DEALLOCATE PREPARE STMT.
END;
The main problem is that the field to create