Inserting Multiple Rows using single SQL statement !!!

February 25, 2008 at 9:12 pm | In SQL | 5 Comments
Tags: ,

I was surprised when i came to know that there is single SQL statement available to insert more than one row at a time. So i decided to write a post on this , inorder to share it with others.

How we generally do :

INSERT INTO TableName(Id,Name)
VALUES (1,'Name1')


INSERT INTO TableName(Id,Name)
VALUES (2,'Name2')

The other way is

INSERT INTO TableName(Id,Name)
SELECT 1,'Name1'
UNION ALL
SELECT 2,'Name2'
UNION ALL
SELECT 3,'Name3'

Cool!!!! Isnt It?????

5 Comments »

RSS feed for comments on this post. TrackBack URI

  1. Cool nive… thanks for ur nice info..awaiting for ur future posts..

  2. To insert multiple records in a table while selecting from another table :

    INSERT INTO TableName1(Id, Name, Constant) SELECT id, name ,’constant value for all records’ from TableName2;

  3. Nice discovery!!

    Can you let me know whether this can be used on oracle 9i/10g as well?

    I tried running the same on 9i but it doesn’t recognise the syntax.

    Thanks,
    Shivam

  4. Hey Shivam, In case u have not yet found an answer to ur problem, it will work this way –

    CREATE TABLE testTable
    ( id INT NOT NULL,CITY VARCHAR(50))

    INSERT ALL
    INTO testTable (id,CITY) VALUES (1,’NEW york’)
    INTO testTable (id,CITY) VALUES (2,’London’)
    INTO testTable (id,CITY) VALUES (3,’Paris’);
    SELECT * FROM dual;

    :-) , worked for me!!!

  5. Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.


Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.