![]() |
SQL Server is a Relational database Management system which is developed by Microsoft and is one of the most used databases in the world. It provides the developer with a set of rich functionalities to create tables, insert data in them, and then manipulate and play with them as and when necessary. In this article, we will be looking at how one can update the top 100 records and understand the various method along with he examples and so on. Introduction of Update in SQL ServerIn SQL Server, the Syntax: UPDATE table_name Explanation:
Ways to Update Top Records in SQL ServerLet us start by creating a table and adding some sample data to the table. We will create a test table with an id and title field. The following query creates the table: Query: CREATE TABLE test(id INTEGER, title VARCHAR(100)); Now that we have the table, let’s insert some data into the data. The following query inserts ten records in the test table: Query: INSERT INTO test (id, title) Output: ![]() Output Explanation: Now that we have the setup in place, lets now go forward to see how we can update the top 100 records of the table. We are going to have a look in this article at two methods to update the top 100 records of the table. Method 1: Using TOP clauseThe TOP clause is used to restrict the number of records returned by the query. Using this in conjunction with the UPDATE clause we can restrict the update to the first n records only. The following code updates the top 3 records of the table. Query: update top (3) test set id=-10; Output: ![]() Output Explanation: As we can see in the above image the id of the top 3 records in the table has been updated to -10. The parenthesis surrounding the number is necessary. Method 2: Using Common Table Expression (CTE)We can make use of Common Table Expression (CTE) to select only the top n records from the table and later update them using the UPDATE clause. However, to make this work, we need to provide an ordering using the ORDER BY clause for the SQL engine to understand what records to return. The following query first creates a CTE of the top 3 records ordered by the id field. The update statement uses the CTE to update the top 3 records as above: Query: WITH CTE AS Output: ![]() Output Explanation: As we can see in the above image the id of the top 3 records in the table has been updated to -10. Example of Update top 100 records in SQL ServerLet’s now use the concepts we have learned in this article in a technical example. First let’s create the table and insert some data inside it. The following query creates an employee table and inserts nine records in it. Query: -- create The above query creates an employee table with three fields and inserts the data for nine employees. The following query returns the initial data in the table: SELECT * FROM EMPLOYEE; Output: ![]() Output Explanation: Now lets try to update the department of the top 3 employees ordered by employee id to ‘Analytics’. For this we can make use of CTE as we did in method 2 to update the records. The following query creates a CTE select the top 3 records in order of their employee id and then the UPDATE statement makes use of the CTE to update the record’s department to ‘Analytics‘: WITH CTE AS Now let’s see the current data in the table. I will use the following query to get the records ordered by the employee id: SELECT * FROM EMPLOYEE Output: ![]() Output Explanation: As we can see in the below image the department of Dave, Jake and Jill has been updated to Analytics from Accounting, Sales and Sales respectively. ConclusionIn this article we covered how we can update the top n records of the table in SQL Server. We had a chance to look at two different methods to go about doing this, first using the TOP clause and the other using CTE. We also how we can use the concepts we learned in this article to a real-life situation through the technical example. |
Reffered: https://www.geeksforgeeks.org
Databases |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |