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 |
|
jackstow
Posting Yak Master
160 Posts |
Posted - 2002-01-31 : 09:17:19
|
| Need some help with this..The problem;A table of articles that are being displayed on a web page and the website owner wants to be able to order them on the page in a specific way and also slot a new article in, delete some old ones or maybe shuffle em all aroundThe (half) solution;A related table with the article ids and an order - sequence_id int PK Identitysequence intrecord_id int-- and a trigger on the sequence table that updates the order when a new record is added or deleted, so there should always be a sequential order with no gaps and no duplicates - CREATE TRIGGER test_update_sequence ON dbo.test_sequence FOR INSERT, DELETE ASDECLARE @record_id intDECLARE @count intDECLARE @sequence int--deal with inserts firstSELECT @count = COUNT(*) FROM INSERTEDIF @count > 0 BEGINselect @sequence = sequence, @record_id = sequence_id from INSERTEDUPDATE test_sequenceSET sequence = sequence + 1WHERE sequence >= @sequence AND sequence_id <> @record_idEND--now to deal with deletesSELECT @count = COUNT(*) FROM DELETEDIF @count > 0 BEGINselect @sequence = sequence, @record_id = sequence_id from DELETEDUPDATE test_sequenceSET sequence = sequence - 1WHERE sequence > @sequence AND sequence_id <> @record_idEND--this seems to work fine, but the problem is with updates. Whatever I try, I always seem to muck up the order when there is an updateMy latest (failed!) attempt;CREATE TRIGGER for_updates ON dbo.test_sequence FOR UPDATES--find top numberDECLARE @top intSELECT TOP 1 @top = sequenceFROM test_sequenceORDER BYsequence DESC--find number updatedDECLARE @updated intSELECT @updated = sequenceFROM INSERTED--find number deletedDECLARE @deleted intSELECT @deleted = sequenceFROM DELETED--now update all the other numbers but not the new number or the last number in the sequenceUPDATE test_sequenceSET sequence = sequence + 1WHERE sequence > @updated AND sequence <> @top AND sequence < @deleted-- any bright ideas out there? Is there an easier way of doing this??Jack |
|
|
wbb1975
Starting Member
23 Posts |
Posted - 2002-07-24 : 08:10:14
|
| I am having the same problem, do you already have a good solution for this problem??GreetingsStefan |
 |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2002-07-24 : 08:33:38
|
| I think you are experiencing a nesting problem on your UPDATE action. Additionally, your trigger is not formed to handle a multi-row update. Run profiler to see exactly what is happening.<O> |
 |
|
|
|
|
|