Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sql resequence of records

Status
Not open for further replies.

jspano

Programmer
Jan 10, 2001
5
US
Here is the deal say we have a table with 2 columns
PartNumber and IDNumber the primary key is on both of them
say you have records like this

PartNumber IDNumber
A 1
A 2
A 5
A 10

I wrote a procedure in SQL to resequence them so you have 1,2,3,4 instead of 1,2,5,10 they must be in the same order as before only in correct numbers. the idnumber field is a float field. the problem is when you have a decimal in the id field say you have .5,1,2,5,10 the cursor gets to the first .5 and tries to make it a 1 and fails because of the primary key constraint. I am using a cursor and fetching rows because I have to update 2 other tables with the new numbers. I have to search the 2 tables for the old and make it the new number.

here is what I am using

DECLARE @SqNum int , @OldSqNum int
Set @SqNum = 1
BEGIN TRANSACTION
DECLARE Resequence_cursor CURSOR
FOR SELECT SequenceNumber
From BillOfMaterialComponents
Where DivisionId = 'A' And AssemblyNumber = '1-373-3'
ORDER BY SequenceNumber
FOR UPDATE
OPEN Resequence_cursor
FETCH NEXT FROM Resequence_cursor
INTO @OldSqNum
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE BillOfMaterialComponents set SequenceNumber = @SqNum
WHERE CURRENT OF Resequence_cursor
Update BillOfMaterialOperations Set SequenceNumber = @SqNum Where DivisionId = 'A' And AssemblyNumber = '1-373-3' And SequenceNumber = @OldSqNum
Update DrawingItems Set BOMSequenceNumber = @SqNum Where DivisionId = 'A' And PartNumber = '1-373-3' And BOMSequenceNumber = @OldSqNum
set @SqNum = @SqNum + 1
FETCH NEXT FROM Resequence_cursor
End
Close Resequence_cursor
DEALLOCATE Resequence_cursor


Any Ideas on how to get this to work?
Thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top