How to update every alternate rows in SQL Server

Posted by Sheonarayan under Sql Server category on | Points: 40 | Views : 4633
I had a requirement where I need to update a field of every alternate rows of my SQL Server database table. After few trial, I came to below code snippet.

Update [UsersName] SET LookingForMe = 1 where (AutoId%2) = 0

Where UsersName is my table name
AutoId is the auto increment column of the database table.
LookingForMe is the column name I wanted to update.

The trick here is that I am getting MOD of AutoId column value and if its 0, I am going to update that row column.

Hope this will be helpful for someone.

Comments or Responses

Login to post response