2016

I have multiple set of data to insert at once, say 4 rows. My table has three columns: Person, Id and Office.

INSERT INTO MyTable VALUES ("John", 123, "Lloyds Office");
INSERT INTO MyTable VALUES ("Jane", 124, "Lloyds Office");
INSERT INTO MyTable VALUES ("Billy", 125, "London Office");
INSERT INTO MyTable VALUES ("Miranda", 126, "Bristol Office");

Can I insert all 4 rows in a single SQL statement?

15
  • 10
    Moderator Note: Please take all discussion about the merits of this question to this meta post. Aug 22, 2013 at 13:39
  • 4
    For oracle sql see stackoverflow.com/a/93724/1452172
    – ono2012
    Jun 26, 2014 at 11:20
  • 1
    @Chinggis6 Yes, trivially possible, just use a select for the column values:
    – Kzqai
    Sep 11, 2017 at 17:24
  • 3
    @Chinggis6 insert into profiles (name, description) select first, 'Auto-generated' from users You seem to be confusing insert and update statement, which are different beasts.
    – Kzqai
    Sep 11, 2017 at 21:21
  • 1
    @Chinggis6 Ah I see. Well, I just recommend using standard insert ... select syntax, it'll get you everything you need and is as flexible as can be wished for. dev.mysql.com/doc/refman/5.5/en/insert.html
    – Kzqai
    Sep 12, 2017 at 13:43

4 Answers 4

2688

In SQL Server 2008 you can insert multiple rows using a single SQL INSERT statement.

INSERT INTO MyTable ( Column1, Column2 ) VALUES
( Value1, Value2 ), ( Value1, Value2 )

For reference to this have a look at MOC Course 2778A - Writing SQL Queries in SQL Server 2008.

For example:

INSERT INTO MyTable
  ( Column1, Column2, Column3 )
VALUES
  ('John', 123, 'Lloyds Office'), 
  ('Jane', 124, 'Lloyds Office'), 
  ('Billy', 125, 'London Office'),
  ('Miranda', 126, 'Bristol Office');
15
  • 435
    And please note that the maximum number of rows in one insert statement is 1000.
    – cryss
    Mar 21, 2013 at 13:39
  • 222
    That should be phrased "the maximum number of rows in one VALUES clause is 1000". It's not the INSERT statement that is limited to 1000 rows.
    – Anon
    Dec 19, 2013 at 17:58
  • 41
    I know this question and answer are old, but I like to mention that there is a functional difference between using the method mentioned in the question and in this answer. The first executes triggers X amount of times with 1 record in the inserted table. The second executes triggers 1 time with x amount of records in the inserted table. Making the second method the best choice for performance assuming you made your triggers work correctly with batch inserts. Apr 24, 2014 at 12:07
  • 8
    This doesn't work with SQL Server 2005, see stackoverflow.com/questions/2624713/…
    – pkr
    Apr 28, 2014 at 16:40
  • 5
    @ahnbizcad Semicolons in T-sql are USUALLY optional, but they are reported to be required in the future. You should get yourself in the habit of using them to terminate each statement--your code will look nicer too IMO.
    – NReilingh
    Dec 9, 2015 at 3:51
890

If you are inserting into a single table, you can write your query like this (maybe only in MySQL):

INSERT INTO table1 (First, Last)
VALUES
    ('Fred', 'Smith'),
    ('John', 'Smith'),
    ('Michael', 'Smith'),
    ('Robert', 'Smith');
10
  • 60
    As of SQL Server 2008, this will work if you replace the double quotes with single ones. Mar 22, 2012 at 13:37
  • 21
    Also works with postgres v9.0
    – suspectus
    Nov 13, 2013 at 10:19
  • 14
    And with SQLite Dec 9, 2013 at 10:13
  • 7
    Only SQLite 3.7.11 onwards. If you cannot guarantee that, use the UNION method shown here: stackoverflow.com/a/5009740/841830 Feb 13, 2014 at 1:12
  • 5
    Muneem, the limit is 1,000 VALUE lines per INSERT statement. Feb 22, 2018 at 18:02
157

NOTE: This answer is for SQL Server 2005. For SQL Server 2008 and later, there are much better methods as seen in the other answers.

You can use INSERT with SELECT UNION ALL:

INSERT INTO MyTable  (FirstCol, SecondCol)
    SELECT  'First' ,1
    UNION ALL
SELECT  'Second' ,2
    UNION ALL
SELECT  'Third' ,3
...

Only for small datasets though, which should be fine for your 4 records.

2
  • Worked at first try, thanks! I could even add extra stuff like: 'Before date: ' + date2str(GETDATE()) + ' after date.' I know it is a bit of a strange comand date2str but is special syntax in my database. Aug 19, 2020 at 16:20
  • Starting from Oracle version 23c, Oracle now supports Value constructors as well. Aug 16, 2023 at 13:59
93

INSERT statements that use VALUES syntax can insert multiple rows. To do this, include multiple lists of column values, each enclosed within parentheses and separated by commas.

Example:

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
5
  • 3
    A better question is why would you want to? Why not just run the cleaner 4 commands all together in one batch. If one row fails your batch fails. if you do them individually grouped together 3 of 4 succeed.
    – M T Head
    Sep 7, 2016 at 16:41
  • 13
    @m-t-head I have an example of why I want to, and will give you 2 reasons. I am inserting into a table which has a data integrity checking trigger. Reason 1 - inserting values separately would violate the integrity check thus rolling back the transaction and returning an error. I am using SQL Server which does not support deferred constraints, so even if instead of a trigger it was a regular constraint, it would still not work. Reason 2 - integrity check is an expensive procedure and I'd rather have it executed once instead of 1000's of times per transaction. I'm still looking for solutions.
    – tomosius
    Jan 21, 2017 at 6:36
  • 1
    you can create one CTE and use insert into YourTable (ID,Name) From select ID,Name From CTE Jul 25, 2017 at 5:15
  • What about this question? stackoverflow.com/questions/51763204/… Basically same concern but has the function of updating in case of duplicate record
    – DayIsGreen
    Aug 9, 2018 at 9:56
  • 3
    @m-t-e-head The main reason is performance, actually. Inserting thousands or millions of rows would be much faster when you bundle them together into reasonably sized batches and using a single INSERT per batch.
    – uncoder
    Sep 13, 2018 at 18:46