Solved

Seeding an order field in a table on insert/update

Posted on 2004-08-28
6
246 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 69

Expert Comment

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

Author Comment

by:paelo
Comment Utility
Thanks for your help Scott.

-Paul.
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

744 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

16 Experts available now in Live!

Get 1:1 Help Now