Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3316462
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:28:19+00:00 2026-05-17T22:28:19+00:00

In the table I want to be able to ‘move’ rows. I have a

  • 0

In the table I want to be able to ‘move’ rows. I have a column called sortid, which automatically on insert is one value higher than the highest value (easily found by selecting top value when sorting my sortid desc).

But I’m a bit confused on what query I’d use for the the ‘up/down’ actions for each row on my ASP.NET page. What query would I use to select the rows immediately ‘below’ or ‘above’ the row to be moved?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:28:20+00:00Added an answer on May 17, 2026 at 10:28 pm

    Moving a record up or down is done by swapping it with the record before or after, respectively.

    If the SortId values are always continuous (i.e. you don’t remove records which would cause a gap), then you can just add or subtract one to get the next or previous record. If the records are not guaranteed to be continuous, then you have to find the record next to the one that you want to move.

    To find the SortId of the record before:

    select max(SortId) from TheTable where SortId < @SortId
    

    To find the SortId of the record after:

    select min(SortId) from TheTable where SortId > @SortId
    

    To swap the two records, you can use this trick that calculates one value from the other:

    update TheTable
    set SortId = @SortId1 + @SortId2 - SortId
    where SortId in (@SortId1, @SortId2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am dynamically creating a table which I want to have clickable rows. When
I have a repeater control outputting some HTML. I want a table which outputs
I have a database table that i want to allow my friends to update.
I have a Database table that I want to display in a DataGridView. However,
I have dbml with single table users i want add partial class for User
I have a couple of triggers on a table that I want to keep
On MS SQL Server 2005, I have a table that I want to replace.
I want to make a table in SqlServer that will add, on insert, a
In a table I want to combine some of the columns, but not in
I know the name of the table I want to find. I'm using Microsoft

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.