3.2. Adding Data with the INSERT Statement

The basic syntax for an INSERT statement looks like this:

INSERT [INTO] <table> [(column_list)] VALUES (data_values)

Let's look at the parts:

  • INSERT is the action statement. It tells SQL Server what it is that we're going to be doing with this statement, and everything that comes after this keyword is merely spelling out the details of that action.

  • The INTO keyword is pretty much just fluff. Its sole purpose in life is to make the overall statement more readable. It is completely optional, but I highly recommend its use for the very reason that they added it to the statement — it makes things much easier to read.

  • Next comes the table into which you are inserting.

  • Until this point, things have been pretty straightforward — now comes the part that's a little more difficult: the column list. An explicit column list (where you specifically state the columns to receive values) is optional, but not supplying one means that you have to be extremely careful. If you don't provide an explicit column list, then each value in your INSERT statement will be assumed to match up with a column in the same ordinal position of the table in order (first value to first column, second value to second column, etc.). Additionally, a value must be supplied for every column, in order, until you reach the last column that both does not accept nulls and has no default (you'll see more about what I mean shortly). In summary, this will be a list of one or more columns ...

Get Professional SQL Server™ 2005 Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.