Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Ex
Posting Yak Master
166 Posts |
Posted - 2005-01-20 : 19:35:42
|
| Hey alli have a slight problem in this triggeri have a table DELAY PK(DelayID) this is what the table looks likedelayID OrderId1..........12..........23..........34..........45..........5(note: the delayid is not always in order i.e could be)delayID OrderId1..........12..........24..........36..........49..........5(but the orderid should be in order at all times)i have this trigger CREATE TRIGGER KEEPORDERONDELETE ON DELAYafter DELETE ASSET NOCOUNT ON UPDATE DELAY SET OrderId = d.OrderId - 1 FROM DELAY d, deleted del WHERE d.OrderId > del.OrderIdgo(it should -1 of every orderid for each deleted row that follows i.e reorder the orderid's so there is no gap in flow of numbers)and when i perform this querydelete from delay where delayid = 2 OR delayid = 4i get thisdelayID OrderId1..........13..........25..........4what i wanted wasdelayID OrderId1..........13..........25..........3anyone help :) want to try to do this withouth using a cursor if possible |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-01-20 : 19:43:35
|
| CREATE TRIGGER KEEPORDERONDELETE ON DELAYafter DELETE ASSET NOCOUNT ON SELECT delayID, identity(int, 1, 1) as OrderIdinto #tempfrom delayUPDATE DELAY SET OrderId = t.OrderIdFROM DELAY d, #temp t WHERE d.delayID = t.delayIDdrop table #tempgoTara |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-01-20 : 19:51:14
|
I changed my mind. This is using the ranking solution. It's better since it doesn't use a temp table:CREATE TRIGGER KEEPORDERONDELETE ON DELAYafter DELETE ASSET NOCOUNT ON update d set orderid = t.orderidfrom [delay] dinner join ( select d1.delayid, orderid = ( select count(*) + 1 from [delay] d2 where d2.delayid < d1.delayid ) from [delay] d1) ton d.delayid = t.delayidgo Tara |
 |
|
|
Ex
Posting Yak Master
166 Posts |
Posted - 2005-01-20 : 20:07:37
|
| thats some nifty code exactly what i was after cheers |
 |
|
|
|
|
|