Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I Update top 100 records in sql server

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?

like image 515
Rajesh Avatar asked Jul 29 '09 06:07

Rajesh


People also ask

How can I update 1 million records 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.

How do you update multiple records in SQL?

just make a transaction statement, with multiple update statement and commit.

How do you update most recent records in SQL?

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.


1 Answers

Note, the parentheses are required for UPDATE statements:

update top (100) table1 set field1 = 1 
like image 73
Umair Ahmed Avatar answered Oct 04 '22 08:10

Umair Ahmed