Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Seeding an order field in a table on insert/update

Posted on 2004-08-28
6
Medium Priority
?
255 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 70

Accepted Solution

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

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

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 70

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

879 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