Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: Insert multiple sets of values in one statement?

Tags:

sql

sqlite

mysql

Is it possible to insert multiple sets of values to a SQLite table in one statement?

I was trying:

INSERT INTO the_table VALUES (1,2,'hi'),(2,0,'foo');

with the different ()s representing different insert sets, but I get an error.

like image 209
Michael Grinich Avatar asked May 30 '09 08:05

Michael Grinich


People also ask

How do you insert multiple records using a single command in SQL?

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 I insert a lot of data into SQL?

If you want to insert more rows than that, you should consider using multiple INSERT statements, BULK INSERT or a derived table. Note that this INSERT multiple rows syntax is only supported in SQL Server 2008 or later. To insert multiple rows returned from a SELECT statement, you use the INSERT INTO SELECT statement.

How do I insert multiple columns in SQL?

Add multiple columns in table. You can use the ALTER TABLE statement in SQL Server to add multiple columns to a table.


2 Answers

Are there only three columns in your table? If not, you could try defining the column names you are setting like so:

INSERT INTO the_table 
       (column1  ,column2  ,column3) 
VALUES (1        ,2        ,'hi'   )
      ,(2        ,0        ,'foo'  )

This convention was introduced in SQL Server 2008 known as the Table Value Constructor. See MSDN's INSERT page for a look at the overall syntax. Also, the INSERT statement can be easily formatted for better readability.

like image 126
Alistair Evans Avatar answered Sep 27 '22 23:09

Alistair Evans


You can do

INSERT INTO the_table 
SELECT 1,2,'hi'
UNION
SELECT 2,0,'foo';
like image 30
cjk Avatar answered Sep 28 '22 00:09

cjk