Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Insert Multiple Rows

Tags:

sql

I want to insert multiple rows in a single table. How can I do this using single insert statement?

like image 250
Janani Avatar asked Sep 11 '12 08:09

Janani


People also ask

Can you insert multiple rows in SQL at once?

INSERT-SELECT-UNION query to insert multiple records Thus, we can use INSERT-SELECT-UNION query to insert data into multiple rows of the table. The SQL UNION query helps to select all the data that has been enclosed by the SELECT query through the INSERT statement.

How do you insert 3 rows in SQL?

If you want to add data to your SQL table, then you can use the INSERT statement. Here is the basic syntax for adding rows to your SQL table: INSERT INTO table_name (column1, column2, column3,etc) VALUES (value1, value2, value3, etc); The second line of code is where you will add the values for the rows.

How do I insert multiple rows in one column in SQL?

The INSERT statement also allows you to insert multiple rows into a table using a single statement as the following: INSERT INTO table_name(column1,column2…) VALUES (value1,value2,…), (value1,value2,…), … In this form, you need to provide multiple lists of values, each list is separated by a comma.


2 Answers

Wrap each row of values to be inserted in brackets/parenthesis (value1, value2, value3) and separate the brackets/parenthesis by comma for as many as you wish to insert into the table.

INSERT INTO example VALUES   (100, 'Name 1', 'Value 1', 'Other 1'),   (101, 'Name 2', 'Value 2', 'Other 2'),   (102, 'Name 3', 'Value 3', 'Other 3'),   (103, 'Name 4', 'Value 4', 'Other 4'); 
like image 172
Tschallacka Avatar answered Oct 05 '22 16:10

Tschallacka


You can use SQL Bulk Insert Statement

BULK INSERT TableName FROM 'filePath' WITH (   FIELDTERMINATOR = '','',   ROWTERMINATOR = ''\n'',   ROWS_PER_BATCH = 10000,    FIRSTROW = 2,   TABLOCK ) 

for more reference check

https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=sql%20bulk%20insert

You Can Also Bulk Insert Your data from Code as well

for that Please check below Link:

http://www.codeproject.com/Articles/439843/Handling-BULK-Data-insert-from-CSV-to-SQL-Server

like image 31
khushal rasali Avatar answered Oct 05 '22 15:10

khushal rasali