Prashanth Jayaram

Overview of the SQL Update statement

October 8, 2018 by

In this article, we’ll walk-through the SQL update statement to modify one or more existing rows in the table. 

In order to modify data in a table, we’ll use an Update statement, a DML (data manipulation language) statement. A SQL update statement comes with a SET clause where we define the column-and-value as a pair of items. In addition, you can enforce the conditional clause. In order to limit the number of rows, we’ll need to set up a where clause. The condition is defined in the where clause that identifies what rows to modify in the table.

After reading this article, you’ll understand the following topics covering how to use a simple SQL update statement

  1. on multiple columns
  2. with computed value
  3. with the compound operator
  4. with the defaults
  5. with SQL joins
  6. with the Where clause
  7. on a remote table
  8. with use Top(n) clause
  9. with CTE (Common-Table-Expression) statements

Running a simple SQL update statement

For this example, we’ll work with Person.Person , so, let’s take a look at the data first. In this case, let’s say, hypothetically, we wanted to change the data of the ModifiedDate column for all rows of the table with the current datetimestamp value.

Let us use the keyword UPDATE, and then the name of the table Person.Person, then use the keyword SET, and after that list the column name ModifiedDate and then the value, in this case, it’s current date timestamp.

Using an update SQL statement with Multiple columns

Here, we’ve to come up with a pair of items, one being the column name, and one being the value, separated by an equal sign. The following example updates the columns Bonus with the value 8000, CommissionPct with the value .30, and SalesQuota by NULL for all rows in the Sales.SalesPerson table.

In this example, the above SQL update statement can be re-written using FROM clause and table alias.

The following example updates rows in the table Sales.SalesPerson. The table alias is created and assigned to “S” in the FROM clause. The alias is also specified as the target object in the UPDATE clause.

Note: Executing a SQL Update statement without the where cases would actually update every record in the table.

Using an update SQL statement with a Where clause

In the following example, we only want to update one row of the Sales.SalesPerson table. In order to do that, we’ll need to use the WHERE clause. The where clause works exactly the same as they did with the SELECT statements. So, let’s add the keyword WHERE, and set a filter that specifies which record to modify. In this case, its BusinessEntityID is equal to 280. Now, we have an update SQL statement with SET, FROM and Where clause keywords.

And now, we see that the columns Bonus, ComissionPct, and SalesQuota have been changed with new values.


Using an update SQL statement with a Top Clause

In the following examples, we can see that the use the TOP clause to limit the number of rows that are modified in the SQL UPDATE statement.

The following example updates the multiple columns of the matching rows in the Sales.SalesPerson table

We can see that the update ran over a random selection of rows.


Note:

  • When the TOP clause is used with any DML operation, the update operation is performed on a random selection of ‘n’ number of rows.
  • You can also use the SET ROWCOUNT option, if you want to update the first set of rows without enforcing random selection using TOP keyword.

Using an update SQL statement with a Top Clause using a CTE

As we all know that the SQL UPDATE statement with a TOP clause doesn’t support an ORDER BY clause but it is possible to get the sorted order of the columns using a CTE (Common Table Expression)

Let us run the same SQL update statement using a CTE

The output signifies the update ran over a order collection of BusinessEntityID .


Using an update SQL statement with Computed values

The following example uses computed value in SQL Update statement. The example increases the value of the Bonus column by 100 and ComissionPct column by 0.005 values for all rows of the BusinessEntityID equal to 288.

Using an update SQL statement with Compound operators

The following example uses the compound operator ‘+=’ and ‘*=’ to add 100 to the Bonus column and multiply 0.002 to CommissionPct column


Using an update SQL statement with a default values

The following example sets the Primary column to its default value ((0)) for all rows that have a value equal to 1

Note: You can find the default definition of the column using the following T-SQL.


Using an update SQL statement with a SQL Joins

This example modifies the Rate column of the entire rows employee where they belong to the ‘Research and Development’ group.

Note: If you’re writing an UPDATE statement and you want to know how many rows might affect, You just need to place SELECT  * FROM, keeping the same WHERE clause, and this will figure out how many rows that going to actually change.


Remote tables using a linked server and OPENQUERY

The following example uses the linked server to update a data on a remote server.


In the following example, rows on the remote table is updated using the OPENQUERY rowset function

Summary

Thus far, we’ve discussed some of simple methods of updating rows using a SQL Update statement in SQL Server and various permutations using conditions, clauses and in other contexts. I hope you enjoyed reading this article and for any questions, please feel to comment below…

See more

To boost SQL coding productivity, check out these free SQL tools for SSMS and Visual Studio including T-SQL formatting, refactoring, auto-complete, text and data search, snippets and auto-replacements, SQL code and object comparison, multi-db script comparison, object decryption and more.





Prashanth Jayaram

Prashanth Jayaram

I’m a Database technologist having 11+ years of rich, hands-on experience on Database technologies. I am Microsoft Certified Professional and backed with a Degree in Master of Computer Application.

My specialty lies in designing & implementing High availability solutions and cross-platform DB Migration. The technologies currently working on are SQL Server, PowerShell, Oracle and MongoDB.

View all posts by Prashanth Jayaram
Prashanth Jayaram
T-SQL

About Prashanth Jayaram

I’m a Database technologist having 11+ years of rich, hands-on experience on Database technologies. I am Microsoft Certified Professional and backed with a Degree in Master of Computer Application. My specialty lies in designing & implementing High availability solutions and cross-platform DB Migration. The technologies currently working on are SQL Server, PowerShell, Oracle and MongoDB. View all posts by Prashanth Jayaram

235 Views