A lot of places I saw people asking for ways to generate Insert statements.
We do have GUI Tools which can extract insert statements for us readily. of the time I choose the MySQLDump way to generate insert statements.
mysqldump -uroot -ppassword –complete-insert –no-create-info DATABASE TABLENAME > TABLENAME.sql
But mind is very unstable and hungry, we don’t stop at one solution.
So to remove my mind’s starvation for the Stored Procedure way to extract Insert statement I created following routine.
As you can see this is really a simple procedure revolves around Information_schema mainly to get details of any table and then fires the simple sql query.
The procedure I named: InsGen
Input parameters:
in_db: Database name of the table for which you want to generate insert statements
in_table: Tabel name
in_file: complete file path [eg: C:/mysqlInserts.sql or /var/lib/data/mysqlInserts.sql]
DELIMITER $$
DROP PROCEDURE IF EXISTS `InsGen` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `InsGen`(in_db varchar(20),in_table varchar(20),in_file varchar(100))
BEGINdeclare Whrs varchar(500);
declare Sels varchar(500);
declare Inserts varchar(2000);
declare tablename varchar(20);set tablename=in_table;
select tablename;
# Comma separated column names – used for Select
select group_concat(concat(‘concat(\’”\’,',’ifnull(‘,column_name,’,””)’,',\’”\’)')) INTO @Sels from information_schema.columns where table_schema=in_db and table_name=tablename;# Comma separated column names – used for Group By
select group_concat(‘`’,column_name,’`') INTO @Whrs from information_schema.columns where table_schema=in_db and table_name=tablename;#Main Select Statement for fetching comma separated table values
set @Inserts=concat(“select concat(‘insert into “, in_db,”.”,tablename,” values(‘,concat_ws(‘,’,”,@Sels,”),’);’) from “, in_db,”.”,tablename,” group by “,@Whrs, ” INTO OUTFILE ‘”, in_file ,”‘”);PREPARE Inserts FROM @Inserts;
EXECUTE Inserts;END $$
DELIMITER ;
Sample output:
I have not considered each and every scenarios yet, but this works for normal tables and it does error if file exists.
My mind is no more hungry atleast in this regard.
I hope it helps.
Related posts:
- Stored procedure to add-remove prefix by rename table mysql Here is one more procedure – (this time) for...
- MySQL Stored procedure – Split Delimited string into Rows This procedure will split a “;” separated column in...
- Stored procedure to Find database objects This procedure lists available database objects under passed database...






Appreciate your effort. But i am getting the following error using ur SP :
Script line: 4 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 ‘\’”\’,ifnull(‘,column_name,’,””)’,’,\’”\’)’)) INTO @Sels f’ at line 12
Cannot figure out the problem. And also is there any way to return the Insert statements as return value instead of writing to a text file ? I am new to MySql
As you’ve copied the code from above snipped it seems a problem with quotes (‘ and `). Please download file available at the end of the page and give it a try.
Regarding not storing output in a file, just remove the section >> ” INTO OUTFILE ‘”, in_file ,”‘”);” from the query and procedure will output the result on prompt.
Replace “set @Inserts….” line with following:
set @Inserts=concat(“select concat(‘insert into “, in_db,”.”,tablename,” values(‘,concat_ws(‘,’,”,@Sels,”),’);’) from “, in_db,”.”,tablename,” group by “,@Whrs, “;”);
MySQL has a feature for profiling query. By default its off, but you can turn it on per MySQL session by issueing “set profiling=1″ on MySQL command prompt.
http://dev.mysql.com/tech-resources/articles/using-new-query-profiler.html
But it has some limitation, first of all, it can be used for debugging while you are in a development mode. Second, it can not be used for query logging purpose.
Major limitation it has is, you can also store 15 queries in MySQL profile and it can be configure upto 100 queries.