501

I want to update the top 100 records in SQL Server. I have a table T1 with fields F1 and F2. T1 has 200 records. I want to update the F1 field in the top 100 records. How can I update based on TOP 100 in SQL Server?

gotqn
  • 42,737
  • 46
  • 157
  • 243
Rajesh
  • 6,269
  • 5
  • 28
  • 23

9 Answers9

831

Note, the parentheses are required for UPDATE statements:

update top (100) table1 set field1 = 1
d219
  • 2,707
  • 5
  • 31
  • 36
Umair Ahmed
  • 11,238
  • 5
  • 33
  • 39
  • 37
    Any idea how to use the `order by` as well? – Joe Phillips Feb 27 '13 at 18:30
  • 11
    @JoePhilllips Use the Martin Smith answer for order by – jjxtra Sep 11 '14 at 19:10
  • 2
    These are not the top 100 records, however, but simply 100 arbitrarily chosen records. Top 100 would include some order to rank the records. – Thorsten Kettner Sep 03 '17 at 01:47
  • 2
    This answers the question "as asked" but TOP is meaningless (and unpredictable) without some ORDER. See the answer further down by Martin Smith. – Andy G Jan 05 '18 at 09:04
  • 1
    btw: parenthesis are important! – Simon_Weaver Mar 06 '18 at 00:43
  • 8
    It's not meaningless if you're trying to update a large table in chunks, but that would require a WHERE clause (such as WHERE field1 IS NULL). So yes TOP is meaningless without *either* ORDER BY or a WHERE clause - and for both cases an index is required for large tables to be efficient. – Simon_Weaver Nov 05 '19 at 22:00
  • I have another reason to disagree with the term `meaningless` being used here. I have a clustered index in my table, if I do not specify an order, the default order is being taken for me. So, if you have an ascending identity column, the records will come in ascending order. – Jamshaid K. Oct 10 '21 at 09:41
  • I'm getting syntax error `UPDATE TOP(2200) posts SET auditor_id = 2 WHERE auditor_id = 4 AND ocr_audit_status = 0 AND ocr IS NULL` can anyone please help – Kunal Rajput Jan 15 '22 at 08:23
  • _So, if you have an ascending identity column, the records will come in ascending order_ usually but not always. You can't rely on order without `order by` – Nick.Mc Apr 21 '23 at 09:46
357

Without an ORDER BY the whole idea of TOP doesn't make much sense. You need to have a consistent definition of which direction is "up" and which is "down" for the concept of top to be meaningful.

Nonetheless SQL Server allows it but doesn't guarantee a deterministic result.

The UPDATE TOP syntax in the accepted answer does not support an ORDER BY clause but it is possible to get deterministic semantics here by using a CTE or derived table to define the desired sort order as below.

;WITH CTE AS 
( 
SELECT TOP 100 * 
FROM T1 
ORDER BY F2 
) 
UPDATE CTE SET F1='foo'
Martin Smith
  • 438,706
  • 87
  • 741
  • 845
  • 94
    You say meaningless but that's not true. I admit that, /usually/, when you're using `TOP` odds are you should be using it with `ORDER BY` because what you're interested in is like the "most" or "least" of something. In other cases, however, you may only be interested in getting one matching record. Like me today! I needed to fix data issues (cycles) one at a time. The entire fix process involved a db script, some user intervention, and some application operations. We didn't care WHICH record was handled first. We just cared that we were handling them one at a time. – MetaFight Mar 19 '12 at 17:10
  • 22
    @MetaFight But then you would have had a `WHERE` clause to exclude previously processed records. The question as written and accepted answer are pretty meaningless. BTW: For using tables as a queue [this is quite a useful link](http://rusanu.com/2010/03/26/using-tables-as-queues/) – Martin Smith Mar 19 '12 at 17:18
  • 1
    @MartinSmith That is correct. My query's `WHERE` clause does indeed filter previously processed records. Thanks for the queue link, but in my situation the order really wasn't important. It's more like a *pool* than a *queue*, but thanks :) I'll keep it in mind. – MetaFight Mar 20 '12 at 09:30
  • 17
    I need to use top without order by so that I can run an asynchronous process. The where clause will not include those that have already been processed, but I can only process so many at a time. So it has a perfectly valid use case. – Jeff Davis Nov 13 '12 at 00:12
  • 1
    @JeffDavis - With a where clause agreed. You are essentially using a table as a heap queue as per the link I posted above. Without a where clause I can't think of a use case. – Martin Smith Nov 13 '12 at 07:51
  • 3
    I've found UPDATE TOP without a where or order by clause useful to populate database fields for testing. Especially after adding a new database column to an existing database. – mvonlintel Feb 25 '13 at 15:39
  • 4
    Sounds meaningful for me: I have a list of the same resources, of which I want to grab one. update top (1) allows me to mark one as in use for me. And I can easily to allocate 10 for my use etc, then query later to see which ones I've been assigned. – Legolas Mar 08 '13 at 14:04
  • 3
    @Legolas - You are just repeating the use case already discussed above of using a table as a heap queue. How do you stop retrieving rows that have already been marked as in use by another process? Presumably you must have a where clause. – Martin Smith Mar 08 '13 at 14:17
  • 12
    @Martin Smith: Let's say you want to update in batches, say 10000 at a time. Seems like a good use for this, and order doesn't matter. How does this "not make sense"? – Jay Sullivan May 05 '14 at 21:14
  • 7
    @notfed This is the same case as has already been discussed to death in the comments above. Your query in that case would *not* look like the one in the accepted answer would it? You would need a `where` clause to avoid processing the same rows again and again. – Martin Smith May 07 '14 at 19:01
  • 3
    @Martin Smith: I agree you would need a where clause. I was referring to your quote, `Without an ORDER BY the whole idea of TOP doesn't make much sense` – Jay Sullivan May 13 '14 at 17:58
  • 1
    @notfed It doesn't make much (if any) sense. Top means highest. Which presupposes some definition of what is high and what is low. This is supplied by the order by. Without that there is nothing to distinguish top from bottom. – Martin Smith May 16 '14 at 17:54
  • @Rainbolt re: you're edit I've rolled it back. Adding a where clause doesn't make the concept of `top` without an `order by` meaningful. You need to know what way is up to distinguish `top` from `bottom`. This is different from saying that there is never a valid use case for the "any" semantics without the order by. Though the question as written is still meaningles as it asks for "the top 100" without providing any way of defining "top as ordered by what". It doesn't ask how can I update a non deterministic 100 rows. – Martin Smith Feb 15 '17 at 22:16
  • 4
    @MartinSmith You've only had three other people try to tell you the same thing, but whatever. – Rainbolt Feb 15 '17 at 23:28
  • 1
    Another use of update top X without order by is when you are testing some import/export function and you want to reset some random rows as to not exported in order to test the import again. Plus, from what I have seen a top without order by normally is not that random as some people think but is indeed sorted by the primary key of the given table that the top rows are selected/updated – kuklei Oct 31 '17 at 13:00
  • 1
    Gathering 100 arbitrary rows is not "meaningless" at all. I was just looking this up because I'm going to run a batch process, and I'm not sure if it works. If It breaks I don't want it to break the entire production database, so I'm limiting it to 100 random clients. Yes, I know that ideally you should be sure that something works _before_ you run it in production and no, that's not possible in this case. – Demonblack Nov 14 '17 at 17:02
  • @Demonblack read the answer properly. It says that TOP is meaningless without some indicator of direction. Not that gathering 100 arbitrary rows is meaningless. Top means “highest”. If you don’t define which way is up how can you tell which is Top? – Martin Smith Nov 14 '17 at 18:35
  • 2
    @Martin Smith Read the comment properly. In my example I do not care what's at the top, I just want to limit the update to 100 rows and no more. I do not care _what exactly_ is being updated. Therefore top is _very_ meanigful even if the "direction" as you say is undefined. But I notice now that several other people have provided similar (almost identical, even) use cases and you just refuse to accept them, so I have no doubt you'll find some way to tell me that I'm wrong. Whatever floats your boat, I guess. – Demonblack Nov 15 '17 at 13:45
  • @Demonblack - it is irrelevant whether or not you find it useful - my comment on TOP is about the English Language word "Top" - which has a clearly defined definition. – Martin Smith Nov 15 '17 at 16:02
  • Why the semicolon at the start of the code? Is this a typo? – Stewart Feb 21 '18 at 17:18
  • 2
    @stewart. No. CTEs require the preceding statement to be terminated with a semicolon. Sql Server normally doesn't require this so this is just to pre-empt people putting it after non semi colon delimited code and seeing an error message. – Martin Smith Feb 21 '18 at 18:35
  • 1
    Figured I'd throw in my example here, I'm thinking of using it to limit an update to 1 row just in case something goes wrong with the query. Hope it works! – Dan Chase Jan 04 '21 at 19:58
  • Agree with everyone else, there are plenty of reasons why you wouldn’t want or need an order by. Like when you are running SQL server in real-time environment, need to do some maintenance work on a transaction table but can't risk blowing vital resources of other threads, just add an update top 1000. You don’t care which 1000 records are updated, as long as all records are picked up eventually. Sticking an order by on it is the last thing you want to do. Top doesn’t mean highest, it means top. Highest is subjective, depending on what data you are interested in. – Vman Aug 19 '23 at 18:18
22

for those like me still stuck with SQL Server 2000, SET ROWCOUNT {number}; can be used before the UPDATE query

SET ROWCOUNT 100;
UPDATE Table SET ..;
SET ROWCOUNT 0;

will limit the update to 100 rows

It has been deprecated at least since SQL 2005, but as of SQL 2017 it still works. https://learn.microsoft.com/en-us/sql/t-sql/statements/set-rowcount-transact-sql?view=sql-server-2017

Claudio B
  • 564
  • 5
  • 12
  • 2
    SET ROWCOUNT affects triggers as well as the command being updated. If you have cascade delete set, it can fail the transaction if more than the rowcount of child rows exist in the child table. – EricI Oct 30 '17 at 22:35
  • With that said, SET ROWCOUNT @RowCountParameter; is valid syntax, whereas SELECT TOP @RowCountParamter * FROM TableName is invalid. If you need to configure the rows being updated, SET ROWCOUNT # is currently the better option, provided you do not have child tables with cascade delete enabled. – EricI Oct 30 '17 at 22:41
  • In SQL Server 2017 it is now possible to use @variable in TOP clause: https://learn.microsoft.com/en-us/sql/t-sql/queries/top-transact-sql?view=sql-server-2017 – Alexandr Zarubkin Apr 24 '19 at 08:28
22
update tb set  f1=1 where id in (select top 100 id from tb where f1=0)
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
hyyxing
  • 277
  • 2
  • 2
4

What's even cooler is the fact that you can use an inline Table-Valued Function to select which (and how many via TOP) row(s) to update. That is:

UPDATE MyTable
SET Column1=@Value1
FROM tvfSelectLatestRowOfMyTableMatchingCriteria(@Param1,@Param2,@Param3)

For the table valued function you have something interesting to select the row to update like:

CREATE FUNCTION tvfSelectLatestRowOfMyTableMatchingCriteria
(
    @Param1 INT,
    @Param2 INT,
    @Param3 INT
)
RETURNS TABLE AS RETURN
(
    SELECT TOP(1) MyTable.*
    FROM MyTable
    JOIN MyOtherTable
      ON ...
    JOIN WhoKnowsWhatElse
      ON ...
    WHERE MyTable.SomeColumn=@Param1 AND ...
    ORDER BY MyTable.SomeDate DESC
)

..., and there lies (in my humble opinion) the true power of updating only top selected rows deterministically while at the same time simplifying the syntax of the UPDATE statement.

Michael Goldshteyn
  • 71,784
  • 24
  • 131
  • 181
3

You can also update from select using alias and join:

UPDATE  TOP (500) T
SET     T.SomeColumn = 'Value'
FROM    SomeTable T
        INNER JOIN OtherTable O ON O.OtherTableFK = T.SomeTablePK
WHERE   T.SomeOtherColumn = 1
Vanderlei Morais
  • 556
  • 6
  • 11
3

this piece of code can do its job

UPDATE TOP (100) table_name set column_name = value;

If you want to show the last 100 records, you can use this if you need.

With OrnekWith
as
(
Select Top(100) * from table_name Order By ID desc
)
Update table_name Set column_name = value;
1

Try:

UPDATE Dispatch_Post
SET isSync = 1
WHERE ChallanNo 
IN (SELECT TOP 1000 ChallanNo FROM dbo.Dispatch_Post ORDER BY 
CreatedDate DESC)
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
1

The TOP qualifier can also be used as limit the the number of rows manually updated incorrectly.

Consider the following UPDATE syntax.

UPDATE TOP (1) table1 SET column1 = 0 WHERE column_pk = '123'

Without the TOP clause, if you are doing a manual update and your mouse text selection only selects from "UPDATE" to just before the "WHERE" clause, then the update is applied to ALL rows. With the TOP clause, only one row would get the undesired update.

The TOP constraint can limit the damage of a missing or incorrect WHERE clause or ORDER BY clause. This can be helpful when it is known that only one or a few rows should be updated.

JohnH
  • 1,920
  • 4
  • 25
  • 32