Inserting multiple rows in a single SQL query? [duplicate]

Asked 2023-09-20 21:01:14 View 357,999

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?

  • Moderator Note: Please take all discussion about the merits of this question to this meta post. - anyone
  • For oracle sql see stackoverflow.com/a/93724/1452172 - anyone
  • @Chinggis6 Yes, trivially possible, just use a select for the column values: - anyone
  • @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. - anyone
  • @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 - anyone

Answers

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');

Answered   2023-09-20 21:01:14

  • And please note that the maximum number of rows in one insert statement is 1000. - anyone
  • 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. - anyone
  • 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. - anyone
  • This doesn't work with SQL Server 2005, see stackoverflow.com/questions/2624713/… - anyone
  • @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. - anyone

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');

Answered   2023-09-20 21:01:14

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.

Answered   2023-09-20 21:01:14

  • 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. - anyone
  • Starting from Oracle version 23c, Oracle now supports Value constructors as well. - anyone

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);

Answered   2023-09-20 21:01:14

  • 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. - anyone
  • @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. - anyone
  • you can create one CTE and use insert into YourTable (ID,Name) From select ID,Name From CTE - anyone
  • What about this question? stackoverflow.com/questions/51763204/… Basically same concern but has the function of updating in case of duplicate record - anyone
  • @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. - anyone