0

Could any one provide an example sql script that generates

Could anyone help me out. Is there a way to create a script that will generate insert statements for the table with already existing data?

There is a table with such a structure -> Country [id (int pk), name(nvarchar)] How will the script for generating such and output look like?

Output:

INSERT INTO country (NAME) VALUES ('canada');

INSERT INTO country (NAME) VALUES ('england');

INSERT INTO country (NAME) VALUES ('italy');

INSERT INTO country (NAME) VALUES ('spain');
6
  • You can easily program this in a loop using most programming languages. Commented Jul 29, 2013 at 14:25
  • i know I could easily write this in c#, however I need this to be done using sql Commented Jul 29, 2013 at 14:26
  • Why does it matter how you generate your script? Commented Jul 29, 2013 at 14:27
  • How about mysqldump with "--extended-insert=FALSE" set? Commented Jul 29, 2013 at 14:29
  • @FloDoe could you be more specific because i'm new to mysql... Commented Jul 29, 2013 at 14:30

3 Answers 3

1

This will do what you wanted:

select concat ("insert into `Country` (`country`) values (\"", country, "\");") from Country order by id;

Sample: http://sqlfiddle.com/#!2/efb4c/2

However you need to tune it for your schema of course.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to insert the existing data again into your table (as you have mentioned in the question) then, you can write an sql like:

INSERT INTO country (NAME) SELECT NAME FROM COUNTRY;

Comments

0

You can use mysqldump to archive the thing you asked, try the following:

mysqldump -u username -ppassword --skip-triggers --compact --no-create-info --extended-insert=FALSE databasename tablename1 tablename2 ... 

If you have huge tables, keep in mind that single inserts will be slower than extended ones.

Also see the docs on mysqldump: mysqldump — A Database Backup Program

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.