Inserting Data

The INSERT reservered word is part of the Data Manipulation Language (DML) defined by Codd in 1970 and is used to add data to a table in the database. Today, I would like to review the different ways to insert data using the AdventureWorks Sample database supplied by Microsoft.

1 – Many times a table is defined with a surrogate key, an autoincrement column. You can specify values for this column by setting the INDENTITY INSERT property of the table. The following code adds a row to the Human Resources Department table with a key value of 17.

 

2 – The next example adds a row to the sample table but allows the system to pick the next key value.

 

3 – Sometimes, there is a need to insert several rows at a time. Before SQL Server 2008, one would use the UNION clause to join recordsets together. The example below demonstrates this technique.

 

4 – With the release of SQL Server 2008, developers can now use the recordset syntax to specify several rows of data to insert at one time. The example below shows this technique.

 

5 – A very powerful but under used syntax allows the developer to insert into a table given the results of a stored proceedure. The example below exhibits this technique.

 

6 – Last but not least, the SELECT INTO variation allows a developer to create a table on the fly. This is very useful when you have a table with a larger number of rows and you want to end up with a smaller subset. Since this statement can be minimally logged, it uses less log space than performing a delete against the orginal table.

 

In summary, working with databases is vital in the information age. That said, adding data is a daily task that developers and administrators perform. Knowing the different variantions of the INSERT statement will help you select the correct syntax to use.

Related posts

2 Thoughts to “Inserting Data”

  1. Oh my goodness! an incredible article dude.

  2. Very great information can be found on web blog.

Leave a Comment