SQL Server > Table- Create, Read, Update, Delete (CRUD)

Inserting multiple records at once in SQL Server

How to insert multiple records into the Sql Server database table at once?


To store multiple records into SQL Server database table, separate batch of columns values by comma (,)

First approach

INSERT INTO MyDetails (FullName, City)
VALUES
('Ram', 'Mumbai') ,
('Shyam', 'Chennai') ,
('Mohan', 'Delhi')

In the above insert statement, we have separated the columns blocks values by”,”. Here are 3 set of columns values for MyDetails database table.

i.    ('Ram', 'Mumbai')
ii.   ('Shyam', 'Chennai')
iii.  ('Mohan', 'Delhi')

Second approach

Another approach of inserting multiple records into the database table, we use UNION ALL separated by SELECT and the column values.

INSERT INTO MyDetails (FullName, City)
SELECT 'Gita', 'Gita nagar'
UNION ALL
SELECT 'Roselin', 'Roselin First'
UNION ALL
SELECT 'Olga', 'Olga Mati'

In this case, three records will get inserted into MyDetails database table.

 Views: 1962 | Post Order: 31



Write for us