The T-SQL INSERT Command Syntax  Hot PDF Print E-mail
Tag it:
Delicious
Furl it!
Digg
NewsVine
Reddit
YahooMyWeb
Technorati
Articles Reviews Structured Query Language
Written by Robin Dewson   
Tuesday, 29 May 2007

Before it is possible to insert data using T-SQL code, you need to be familiar with the INSERTcommand and its structure.

The INSERT command is very simple and straightforward in its most minimal form, which is all that is required to insert a record.



INSERT [INTO]
{table_name|view_name}
[{(column_name,column_name,...)}]
{VALUES (expression, expression, ...)}


Obviously, we are required to start the command with the type of action we are trying to perform, for example, insert data. The next part of the command, INTO, is optional. It serves no purpose, but you will find some do use it to ensure their command is more readable.

The next part of the statement deals with naming the table or the view that the insertion has to place the data into. If the name of the table or view is the same as that of a reserved word or contains spaces,
we have to surround that name with square brackets or double quotation marks.

However, it is better to use square brackets, because there will be times you wish to set a value such as Acme’s Rockets to a column data, which can be added easily by surrounding it by double quotation marks.

I cannot stress enough that really, there is nothing to be gained by using reserved words for table, views, or column names. Deciding on easy-to-use and unambiguous object names is part of a good design.

Column names are optional, but it is best practice to list them to help to have reliable code, as this ensures that data is only inserted into the columns into which you want it to be inserted.

Therefore, it will be necessary to place the column names in a comma-delimited list. The list of column names must be surrounded by parentheses, (). The only time that column names are not required is when the INSERT statement is inserting data into every column that is within the table in the same order as they are laid out in the table.

However, this is a potentially dangerous scenario. If you build an INSERT command which you then saved and used later, you expect the columns to be in a specific order because that is the way they have always been.

If someone then comes along and adds a new column, or perhaps alters the order, your query or stored procedure will either not work or give erroneous results, as values will be added to the wrong columns.

Therefore, I recommend that you always name every column in anything but a query, which is built, run once, and thrown away.

The VALUES keyword, which precedes the actual values to be entered, is mandatory. SQL Server needs to know that the following list is a list of values, and not a list of columns.

Therefore, you have to use the VALUES keyword, especially if you omit the list of columns as explained previously.

Finally, you will have a comma-separated list surrounded by parentheses covering the values of data to insert. There has to be a column name for every value to be entered.

To clarify, if there are ten columns listed for data to be entered, then there must be ten values to enter.


User reviews

There are no user reviews for this item.

Add new review




Powered by jReviews

Last Updated ( Friday, 04 January 2008 )
 
< Prev   Next >