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 |
|
qutesanju
Posting Yak Master
193 Posts |
Posted - 2009-02-01 : 05:57:43
|
| hi i have one temp table like thisCREATE TABLE #temp (eqno int, counter int ) delete from #temp Insert into #temp values(50001580,1)Insert into #temp values(50001580,1)Insert into #temp values(50001581,1)Insert into #temp values(50001581,1)Insert into #temp values(50001582,1)Insert into #temp values(50001582,1)select * from #tempdeclare @v_rowcount intdeclare @i intset @i=0set @v_rowcount= (select count(*) from #temp)--print @v_rowcountWHILE (@v_rowcount<>0) BEGIN print @v_rowcount print '------------' --print @i -- insert into #temp (counter)values(@i)-- update #temp -- SET counter = @i set @i=@i+1 update #temp SET counter = @i print @i set @v_rowcount=@v_rowcount-1 ENDhere table is not getting updated even i checked by printing he values of variables those are flowing correctly is there any prob with update statement?and later on i need to reset this counter per equipment wiseto 1like50001580,150001580,250001581,1-- reset when eqno changes50001581,250001582,1-- reset when eqno changes50001582,2 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-01 : 07:11:56
|
| try like this without using whileupdate ttset counter = t.ridfrom #temp ttinner join (select row_number()over(partition by eqno order by eqno)as rid , eqno from #temp) t on t.eqno = tt.eqno |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-01 : 11:22:46
|
quote: Originally posted by bklr try like this without using whileupdate ttset counter = t.ridfrom #temp ttinner join (select row_number()over(partition by eqno order by eqno)as rid , eqno from #temp) t on t.eqno = tt.eqno
No need of join:Update tset t.counter = t.ridfrom (select row_number()over(partition by eqno order by eqno)as rid , * from #temp) t |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-01 : 11:41:09
|
In SQL 2000:--Create table variableDeclare @t table(eqno int,counter int) --Insert sample dataInsert into @t values(50001580,1)Insert into @t values(50001580,1)Insert into @t values(50001581,1)Insert into @t values(50001581,1)Insert into @t values(50001582,1)Insert into @t values(50001582,1)--Make UniqueSelect IDENTITY(int,1,1) as ID,* into #K from @t--Roll the WheelUpdate ZSet Z.Counter = Z.ROW_IDfrom(select eqno,Counter,(Select Count(*) from #K Where eqno = t.eqno and ID <= t.ID)as ROW_IDfrom #K t)Z--Desired outputSelect Eqno,Counter from #K--outputEqno Counter50001580 150001580 250001581 150001581 250001582 150001582 2 |
 |
|
|
qutesanju
Posting Yak Master
193 Posts |
Posted - 2009-02-02 : 04:57:10
|
| it's giving error --in sql server 2000Derived table 'Z' is not updatable because a column of the derived table is derived or constant. |
 |
|
|
ra.shinde
Posting Yak Master
103 Posts |
Posted - 2009-02-02 : 06:11:08
|
| Declare @t table(eqno int,counter int) --Insert sample dataInsert into @t values(50001580,1)Insert into @t values(50001580,1)Insert into @t values(50001581,1)Insert into @t values(50001581,1)Insert into @t values(50001581,1)Insert into @t values(50001582,1)--Make UniqueSelect IDENTITY(int,1,1) as ID,* into #K from @t--Roll the WheelUpdate #kSet Counter = Z.ROW_IDfrom(select ID as ID1,eqno,Counter,(Select Count(*) from #K Where eqno = t.eqno and ID <= t.ID)as ROW_IDfrom #K t)ZWHERE ID = z.ID1--Desired outputSelect Eqno,Counter from #kdrop table #KRahul Shinde |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-02 : 09:18:54
|
| [code]Update mSet m.Counter = Z.ROW_IDfrom #K minner join(select eqno,Counter,(Select Count(*) from #K Where eqno = t.eqno and ID <= t.ID)as ROW_IDfrom #K t)Zon m.eqno = Z.eqno Select eqno,Counter from #K[/code] |
 |
|
|
|
|
|
|
|