Question : use partitions in old code

how would you incorporate sql 2005 partition idea in this cursor to make it more efficient?

thanks
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
Create Proc
Declare Variables
put values in variables
Insert 4 Temp Tables & Populate (#SI_OVER_I is one of them)

Declare @IncomingId int
Declare @SI_ID int
DECLARE BenchCursor CURSOR FOR
	SELECT DISTINCT  IncomingId , SI_ID FROM  #SI_OVER_I order by SI_ID
Open BenchCursor
Fetch Next From BenchCursor Into @IncomingId, @SI_ID
	While @@Fetch_Status  = 0
Begin

	UPdate #SI_OVER_I 
	Set SI_Seq = (Select max(SI_Seq) from #SI_OVER_I where IncomingId = @IncomingId  ) + 1 
	from #SI_OVER_I where IncomingId = @IncomingId  and SI_ID = @SI_ID

Fetch Next From BenchCursor Into @IncomingId, @SI_ID
End

Close BenchCursor
Deallocate BenchCursor 

Create Temp table to process the data
Create Final temp table to dump the needful data
Then update all the final data according to business rules

Answer : use partitions in old code

do not use a cursor for this its simple set processing

Create Proc
Declare Variables
put values in variables
Insert 4 Temp Tables & Populate (#SI_OVER_I is one of them)



UPdate A
   Set SI_Seq = b.maxsi + y.rn
  from #SI_OVER_I as A
 Inner Join (select incomingid,max(si_seq) as maxsi
               from #si_pver_i
              group by incomingid) as B
    on a.incomingid=b.incomingid
 Inner Join (select incomingid,row_number() over(order by si_id) as rn
               from (select distinct incomingid,si_id from #si_over_i) as x  
             ) as Y
    on a.incomingid=y.incomingid
 where IncomingId = @IncomingId  and SI_ID = @SI_ID


Create Temp table to process the data
Create Final temp table to dump the needful data
Then update all the final data according to business rules
Random Solutions  
 
programming4us programming4us