Inserting multiple records using single insert statement

Friday, 15 May 2009

An Interesting topic with Insert statement,

Is it possible to insert multiple records using one single insert statement?

Most of the programmers will say 'NO' But

Let's see,

IF EXISTS (SELECT NAME FROM SYSOBJECTS WHERE NAME='EMP' AND TYPE='U')

DROP TABLE EMP

CREATE TABLE EMP(ID INT, NAME VARCHAR(10))

INSERT INTO EMP

SELECT 1,'ANSARI'
UNION ALL
SELECT 2,'MAGESH'
UNION ALL
SELECT 3,'ANOOP'
UNION ALL
SELECT 4,'SARAVANA'
UNION ALL
SELECT 5,'PRASANNA'
UNION ALL
SELECT 6,'FAHAD'
UNION ALL
SELECT 7,'KARTHICK'
UNION ALL
SELECT 8,'VIJAY'
UNION ALL
SELECT 9,'SAHIL'

SELECT * FROM EMP



1 comments:

anoop ravi said...

Hi folks,
I would like to add some more info to Ansari's example.
In SQL Server 2008,it is possible in another way also.
For example:
INSERT INTO EMP(ID,NAME)
VALUES (1,'ANSARI'),
(2,'MAGESH'),
(3,'ANOOP'),
(4,'SARAVANA'),
(5,'PRASANNA')


Thanks.
"I am sharing what was shared with me"