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 6960693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:26:04+00:00 2026-05-27T15:26:04+00:00

Let’s say I have a Product table in a shopping site’s database to keep

  • 0

Let’s say I have a Product table in a shopping site’s database to keep description, price, etc of store’s products. What is the most efficient way to make my client able to re-order these products?

I create an Order column (integer) to use for sorting records but that gives me some headaches regarding performance due to the primitive methods I use to change the order of every record after the one I actually need to change. An example:

Id    Order
5     3
8     1
26    2
32    5
120   4

Now what can I do to change the order of the record with ID=26 to 3?

What I did was creating a procedure which checks whether there is a record in the target order (3) and updates the order of the row (ID=26) if not. If there is a record in target order the procedure executes itself sending that row’s ID with target order + 1 as parameters.

That causes to update every single record after the one I want to change to make room:

Id    Order
5     4
8     1
26    3
32    6
120   5

So what would a smarter person do?

  • I use SQL Server 2008 R2.

Edit:

I need the order column of an item to be enough for sorting with no secondary keys involved. Order column alone must specify a unique place for its record.

In addition to all, I wonder if I can implement something like of a linked list: A ‘Next’ column instead of an ‘Order’ column to keep the next items ID. But I have no idea how to write the query that retrieves the records with correct order. If anyone has an idea about this approach as well, please share.

  • 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-27T15:26:05+00:00Added an answer on May 27, 2026 at 3:26 pm
    Update product set order = order+1 where order >= @value changed
    

    Though over time you’ll get larger and larger “spaces” in your order but it will still “sort”

    This will add 1 to the value being changed and every value after it in one statement, but the above statement is still true. larger and larger “spaces” will form in your order possibly getting to the point of exceeding an INT value.

    Alternate solution given desire for no spaces:

    Imagine a procedure for: UpdateSortOrder with parameters of @NewOrderVal, @IDToChange,@OriginalOrderVal

    Two step process depending if new/old order is moving up or down the sort.

    If @NewOrderVal < @OriginalOrderVal --Moving down chain 
    
    --Create space for the movement; no point in changing the original 
        Update product set order = order+1 
        where order BETWEEN @NewOrderVal and @OriginalOrderVal-1;
    
    end if
    
    If @NewOrderVal > @OriginalOrderVal --Moving up chain
    
    --Create space  for the momvement; no point in changing the original  
      Update product set order = order-1 
      where order between @OriginalOrderVal+1 and @NewOrderVal
    end if
    
    --Finally update the one we moved to correct value
    
        update product set order = @newOrderVal where ID=@IDToChange;
    

    Regarding best practice; most environments I’ve been in typically want something grouped by category and sorted alphabetically or based on “popularity on sale” thus negating the need to provide a user defined sort.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have table with column 'URL' whrere I store urls like this
Let's say i have a mysql database table 'article' with the following fields: id
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have this code: <p dataname=description> Hello this is a description. <a
Let's say I have this MySQL table: OK.. see the type field? Type 0
Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,

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.