Solved

Seeding an order field in a table on insert/update

Posted on 2004-08-28
6
249 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:
Scott Pletcher 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:Scott Pletcher
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 69

Expert Comment

by:Scott Pletcher
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:Scott Pletcher
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

860 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