Insert Multiple Values in Table SQL Server 2008

In this article showing you how to insert multiple values into a table using a single query in SQL Server
  • 2310
Introduction

In this article showing you how to insert multiple values into a table using a single query in SQL Server. Currently when developers have to insert any value into the table they have to write multiple insert statements. First of all this is not only boring, it is also very much time consuming as well. Additionally, one has to repeat the same syntax so many times. It is possible to write the INSERT INTO statement in two forms. First we can use a simple insert query and another way is to use a union all to do it. I have constructed a query that inserts multiple values in table. Let's take a look at a practical example. The example is developed in SQL Server using the SQL Server Management Studio. Structure of table is shown in below image.

insert 1.jpg
 

1. Insert multiple values in table using single INSERT statement

The syntax for inserting multiple values is:

INSERT INTO table-name (columns not mandatory) query

 
create table yit(ID int , Name varchar(15), Salary int, )
select * from yit;
insert into yit;
insert into yit values (1, 'Arjun', 15000),(2, 'Dev', 16000),(3, 'ram', 10000), (4, 'BPS', 18000)

Now Press F5 to run the query and see the result, as in:

ar 2.jpg
 

2. Insert multiple values in table using UNION ALL

The syntax for inserting multiple values is:

INSERT [EmployeeDetail](columns not mandatory)
SELECT  columnValue1,columnvalue2....columnValueN
UNION ALL
...........
...........

Now the following is the simple example to insert multiple values into the table using a UNION ALL Query:

create table ttt(ID int , Name varchar(15), Salary int, )
insert into ttt
select 1, 'arjun', 15000 union all
select 2, 'Dev', 16000 union all
select 3, 'ram', 10000 union all
select 4, 'BPS', 18000
select * from ttt;

 Now Press F5 to run the query and see the result, as in:

 ar_1.jpg


© 2019 DotNetHeaven. All rights reserved.