Quantcast
Channel: CodeFari
Viewing all articles
Browse latest Browse all 265

Insert multiple rows in single SQL Query

$
0
0
There are many different way you can insert multiple rows in single SQL Query.
When I was fresher, many times I face this question in interview, can you please write a script which can insert multiple rows. See following scripts which may help you.

Suppose we have a table Customer which contains FirstName, LastName.


CREATETABLE#CUSTOMER(
FirstNameVARCHAR(50),
LastNameVARCHAR(50)
)


Script 1:


INSERTINTO#CUSTOMER(FirstName,LastName)
VALUES ('Dilip','Singh'),('Ashish','Singh'),('Vipul','Bhatt')

SELECT*FROM#CUSTOMER


Script 2:


INSERTINTO#CUSTOMER(FirstName,LastName)
SELECT'Dilip','Singh'
UNIONALL
SELECT'Ashish','Singh'
UNIONALL
SELECT'Vipul','Bhatt'

SELECT*FROM#CUSTOMER


Script 3:


INSERTINTO#CUSTOMER(FirstName,LastName)
EXEC(
                        'SELECT ''Dilip'',''Singh''
                        SELECT ''Ashish'',''Singh''
                        SELECT ''Vipul'',''Bhatt'''
            )

SELECT*FROM#CUSTOMER





Viewing all articles
Browse latest Browse all 265

Trending Articles