Updating Data

The UPDATE reservered word is part of the Data Manipulation Language (DML) defined by Codd and is used to modify existing row data in one table in the database.

Today, I would like to review the different ways to update data using the Adventure Works Sample database supplied by Microsoft. Each example has a SELECT statement to identify the number of rows affected before executing the UPDATE.

1 – The simplest form of the UPDATE statement modifies one column in a given table filtered by one where clause. The following example updates the employee table adding 8 hours to the VacationHours column for employee who has less than or equal to 80 hours remaining.

 

2 – A more complex form of the UPDATE statement modifies several columns filtered by several where clauses. The following example finds all single male employees in the Adventure Works company. Each employee has their Vacation and Sick Leave hours reduced by 4 to save the company money during these economic hard times.

 

3 – The most complex form of the UPDATE statement updates one or more columns using several join tables and where clauses to get the resulting set. Transaction SQL supports an non-standard FROM key word that makes the query simpler; However, this form is not ANSI compitable.

If you are writing code that might have to be executed on another database platform in the future, do not use this form. The ANSI Standard syntax can be used with a EXISTS sub-query to achieve the same results.

Please see both the ANSI and TSQL versions of the UPDATE statement below.

 

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

Related posts

One Thought to “Updating Data”

  1. I was reading through some of your content on this internet site and I conceive this site is real instructive! Retain putting up.

Leave a Comment