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?
DECLARE @Rows INT, @BatchSize INT; -- keep below 5000 to be safe SET @BatchSize = 2000; SET @Rows = @BatchSize; -- initialize just to enter the loop BEGIN TRY WHILE (@Rows = @BatchSize) BEGIN UPDATE TOP (@BatchSize) tab SET tab. Value = 'abc1' FROM TableName tab WHERE tab. Parameter1 = 'abc' AND tab.
just make a transaction statement, with multiple update statement and commit.
Here is the syntax that we can use to get the latest date records in SQL Server. Select column_name, .. From table_name Order By date_column Desc; Now, let's use the given syntax to select the last 10 records from our sample table.
Note, the parentheses are required for UPDATE statements:
update top (100) table1 set field1 = 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With