Solved

Seeding an order field in a table on insert/update

Posted on 2004-08-28
6
247 Views
Last Modified: 2008-01-09
I have a couple tables which have sequential, non-PK integer fields which describes the order of records.  

Example from one table:
101
102
103
104

I have a procedure for this table which handles inserts and updates.  Sometimes the new record will be inserted at the end (max + 1), but sometimes it will be inserted in the middle somewhere.  An updated record may move from one position to another.

What is the most efficient way to re-seed the rest of the record accordingly with the aforementioned operations.  I'll place this at the end of the stored procedure.  

-Paul.
0
Comment
Question by:paelo
  • 4
  • 2
6 Comments
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 400 total points
ID: 11932419
You'd be better off avoiding that somehow, but if you really have to do it, UPDATE all records after that by adding 1, for example:

Original rows:
101; 102; 103; 104.

Adding "new" 103::

First do this update:
UPDATE yourTable
SET seqNum = seqNum + 1
WHERE seqNum >= 103

Then insert the new row:
INSERT INTO yourTable (seqNum, ...) VALUES(103, ...)

So the final result will be:

101; 102; +103+; 104(was 103); 105(was 104).
0
 
LVL 9

Author Comment

by:paelo
ID: 11933291
Thanks for your reply Scott.

This is the type of statement I've been using on an interim basis, but it won't work if I wish to update the order of a current record.  Say I want to move 103 to 105, then the current 104 & 105 have to be reseeded to 103 & 104, respectively.  Or it's possible for 105 to move to 103, and so on.

I have a couple ideas but I'd like to avoid using a cursor to re-seed the list so I'm interested in a solution involving 1 or 2 UPDATE statements.

The problem with this table is that the order needs to be rather arbitrary (not dependent on actual data fields within the table) so I can't think of another solution for having them appear in the proper order.  I'm open to suggestions, however.

Thanks again,
-Paul.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 11933623
If, rather than adding a row, you are moving a row, qualify the update accordingly:

UPDATE yourTable
SET seqNum = seqNum - 1
WHERE seqNum BETWEEN 104 AND 105

UPDATE yourTable
SET seqNum = 105
WHERE seqNum = 103
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 69

Expert Comment

by:ScottPletcher
ID: 11933627
Btw, I agree, you should definitely avoid a cursor.
0
 
LVL 9

Author Comment

by:paelo
ID: 11934195
Thanks for your help Scott.

-Paul.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 11934360
You probably have one already, but just to be sure, create an index on the "sequence number" column if you don't already have one.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now