Author |
Topic |
SamC
White Water Yakist
3467 Posts |
Posted - 2004-07-18 : 20:07:01
|
I've got a table with a column of sequence numbers.After deleting a few rows, I'd like to run an update query that will keep the rows in the original sequence, but resequence the column starting with 1.Sample Data:texta, 1textb, 2textc, 3textd, 4texte, 5After deleting 2 rows:textb, 2textc, 3texte, 5After the resequencing updatetextb, 1textc, 2texte, 3 |
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-07-18 : 21:11:41
|
Why don't you just select the remaining ID's into a temp table with an identity column, then update the id row to be whatever the corresponding identity column is in the temp table.MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2004-07-18 : 21:22:09
|
Could do that. I was thinking there might be a temp tableless form ofdeclare @cnt intset @cnt = 0update MyTableSET @cnt = MySequence = @cnt + 1WHERE ....But I run out of imagination trying to come up with a JOIN or ORDERBY to give the desired result. |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-07-18 : 21:32:59
|
Create Table #tempTable (data nvarchar(10), seq int)Insert into #tempTable Values('textb',2)Insert into #tempTable Values('textc',3)Insert into #tempTable Values('texte',5)Select *, newSeq = (Select count(*)+1 From #tempTable Where seq<A.seq) From #tempTable as ADrop Table #tempTableCorey |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2004-07-18 : 21:56:02
|
Thanks Corey...UPDATE ASET Sequence = (select count(*) + 1 from mytable where Sequence < A.Sequence)FROM MyTable A |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-07-18 : 22:01:39
|
DUH! :)Create Table #tempTable (data nvarchar(10), seq int)Insert into #tempTable Values('textb',2)Insert into #tempTable Values('textc',3)Insert into #tempTable Values('texte',5)UPDATE ttSET tt.seq = (SELECT COUNT(seq)+1 FROM #tempTable WHERE seq<tt.seq) FROM #tempTable ttSELECT * FROM #tempTableDrop Table #tempTableMeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-07-18 : 22:03:40
|
Something wrong?? Corey |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-07-18 : 22:07:08
|
The funny thing is I've posted a solution just like this on here several times. lolMeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2004-07-18 : 22:18:17
|
My excuse: It's Sunday night, I'm tired.Seems to me there's also a JOIN solution that might be faster (I don't have a performance issue, this is for the heck of it.) |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-07-18 : 22:23:06
|
Create Table #tempTable (data nvarchar(10), seq int)Insert into #tempTable Values('textb',2)Insert into #tempTable Values('textc',3)Insert into #tempTable Values('texte',5)Select A.data, A.seq, newSeq = count(*)From #tempTable as A, #tempTable as BWhere A.seq >= B.seqGroup By A.data, A.seqDrop Table #tempTableCorey |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2004-07-18 : 22:32:30
|
Great Select, but it's not an update... |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-07-18 : 22:36:13
|
I like my example. I just like saying update tt though.MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-07-18 : 22:49:06
|
yeah yeah... details [shrug]Corey |
|
|
|