Question:
How to insert multiple rows of data using INSERT INTO statement?
r.meeray
2008-07-16 10:48:37 UTC
I need to create a dropdownlist that list down countries...
I have a list of countries (starting from Afghanistan....till Zimbabwe )

how to write an INSERT INTO statement so that i can insert this list in one shot....

the table name - tblCountry
the column name - strCountry

can anyone help me with this?

thanks in advance.
Five answers:
Mikhus
2008-07-16 11:03:22 UTC
the code below is written for MySQL RDBMS:



INSERT INTO tblCountry (strCounty) VALUES ('US'),('GB'),('Russia'),...



if you're running this statement on a production database don't forget to add LOCK TABLES tblCountry WRITE; before statement and UNLOCK TABLES; after statement to prevent data integrity to be broken.
2008-07-16 11:17:38 UTC
I'm not sure you can do that with just any dbms. But, I think MS SQL Server 2008 lets you do the following:



INSERT INTO tblCountry(strCountry) values ('Country1'), ('Country2'), ('Country3');



I don't think this insert statement is valid in other dbms's though, I might be wrong. Your best bet, if the previous doesn't work, is to write a small script or program to loop through the country list you have and create your insert statements or insert the data into the table for you.
?
2016-05-25 06:20:49 UTC
MS Excel Rows = 65536 (comes from raising 2 to the power of 16) Cols = IV = 256 (comes from raising 2 to the power of 8) .
the_one_true_dave_anderson
2008-07-16 11:09:14 UTC
If you're using MSSQL, the syntax would be:



INSERT INTO tblCountry (strCountry)

SELECT 'Afghanistan'

UNION ALL

SELECT 'Algeria'

UNION ALL

SELECT 'Albania'



with as many 'union all's between the selects as needed.
destinys child
2008-07-16 10:53:34 UTC
you must use commas

for example



insert into tblcountry (a,b,c,d,e,f,g) values (1,2,3,4,5,6,7)



etc. something like this.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...