Insert multiple values sets into a table in one command

4

February 28, 2013 by Kenneth Fisher

The INSERT command has two distinct ways to load data into a table.

INSERT INTO Table (field1, field2) VALUES ('string1','string2')

And

INSERT INTO Table (field1, field2)
SELECT field1, field2 FROM Table2

Both of these are very handy and have been around as far back as I can remember. Historically you would use the INSERT INTO VALUES to load one row, and the INSERT INTO SELECT to load multiple rows. However as of SQL 2008 the INSERT INTO VALUES was expanded to allow multiple inserts at once.

INSERT INTO Table (field1, field2) VALUES
     ('string1','string2'),
     ('stringA','stringB') 

This lets you create a single command that will insert multiple rows into a table at once. Basically you list multiple (valuelist)s delimited by commas. There may be a high end to the number of (valuelist)s that you can put in a single command but I haven’t found it yet.

About these ads

4 thoughts on “Insert multiple values sets into a table in one command

  1. ravi900kumar says:

    sir i want to insert table2 auto increment id also in table1 how it is possible

  2. Carlos Senna says:

    Kenneth, thank you for your posts that are very helpfull, but regarding inputing several rows in one command, I encountered a problem. I am running EMS light to run the SQL scripts and it is returning a SQL error code -104 (token unknown). This token is the comma at end of the line.

    • I’ve never used EMS before but I’m going to guess that it wants to treat each line as a separate piece of code. Try removing all carriage returns and see if that fixes it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 334 other followers

%d bloggers like this: