Top Keyword With Update Statement In SQL

In this article i will explain how to use the top keyword in a update query to update only the top few records for a specific condition.
  • 1445

Introduction

Basically, most of us know about top, we can use it in a select query to get the top element from the query. But TOP can also be used with Insert, Delete and Update commands for the table as well. We will see how we can use TOP with Update statement.

Syntax

Update top (N) <Table Name> set Colmn1=<value>

where N represents number of records.

Example

We have table named Employee.

CreateTable-in-sql.jpg


To update TOP 2 employee from Employee Table and update salary from 20,000 to 30,000 and L_Name to XXX.

TopKeyword-with-Update-in-SQL.jpg

OUTPUT:

Update-TopRows-in-sql.jpg


© 2013 dotNetheaven. All rights reserved.