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 |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2002-05-31 : 09:07:01
|
| Heath writes "Hello, I am having some problems with a SQL query for SQL server 2000/7. I can accomplish what I want to do in straight ASP, but I figured there had to be a better way to do it in the database alone. I am running both SQL Server 2000 and 7.0 on Win2000 clients and NT and Win2000 Servers. Not sure what Service packs we have at the office.Here is the scenario. To make the example simple let's say I have a table with 10 records. These records contain a key field that links back to their parent record that is in another table. There is also another key field that contains the sequence value for the records (the two together establish the primary key of course). I need to delete the record that has the sequence value of '4'. Once that is done I now have a gap in my sequencing (1,2,3,5,6,7,8,9,10). Is there a way in SQL to say something of the following?:UPDATE thetable SET (my sequence value to one less than my current value) WHERE (the sequence value is between 5 AND 10)This would then shore up the gap and give us 1,2,3,4,5,6,7,8,9Thanks for your help!" |
|
|
dsdeming
479 Posts |
Posted - 2002-05-31 : 10:21:39
|
| TryUPDATE thetable SET mysequencevalue = mysequencevalue - 1WHERE mysequencevalue between 5 AND 10 |
 |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2002-05-31 : 10:38:25
|
| Typically, these types of sequence numbers are used for ordering a resultset later.If that is the case, the gap won't make a difference.If that's not the case, it may be easier leave the gap to 'generate' a new sequence number at querytime based on the order by mysequencevalue.<O> |
 |
|
|
MakeYourDaddyProud
184 Posts |
Posted - 2002-05-31 : 11:49:15
|
| That's assuming 10 is the maximum number. What if that and the 'gap' number is not known?I wanted a subquery to isolate the rows having IDs 1 and 2 ahead, we can then drill down to theexact rows with 1 ahead is a null and 2 and not null i.e. a GAP. Here goes...drop table #thetablegocreate table #thetable (id int, -- primary key link_id int, -- foreign ket sumdata varchar(123) )Goinsert into #thetable values (1, 20, "Mum")insert into #thetable values (2, 20, "Dan")insert into #thetable values (3, 20, "Sister")insert into #thetable values (5, 20, "Brother")insert into #thetable values (6, 20, "Son")gothen we have t0:1) left outer join twice 1 for the next row and 1 for the second2) filter to the exact condition i.e. t2.id null and t3.id not null i.e. a gap3) bind a subquery to delete on a scalar tuple valueSo we have: UPDATE #thetableSET id = id - 1WHERE id > (SELECT t1.id FROM #thetable t1 LEFT JOIN #thetable t2 ON t2.id = t1.id + 1 LEFT JOIN #thetable t3 ON t3.id = t1.id + 2 WHERE t2.id IS NULL AND t3.id IS NOT NULL)select * from #thetableid link_id sumdata----------- ----------- -------1 20 Mum2 20 Dan3 20 Sister4 20 Brother5 20 Son |
 |
|
|
|
|
|
|
|