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 |
|
pras2007
Posting Yak Master
216 Posts |
Posted - 2009-10-11 : 09:36:40
|
| Hello All,Does anybody know how to convert this cursor that has two scenarios into a faster operations that does not use a cursor? The cursor code is shown below:declare @processdate datetime -- current as of datedeclare @RMS money -- current RMSdeclare @inslayer varchar(25)-- current ins_layerdeclare @claimnumber varchar(10)-- current claim Numberdeclare @ipc money -- current IPCdeclare @xpc money -- current EPCdeclare @tpc money -- current TPCdeclare @prevprocessdate datetime -- previous processdatedeclare @prevRMS money -- previous RMSdeclare @previnslayer varchar(25)-- previous ins_layerdeclare @previpc money -- current IPCdeclare @prevxpc money -- current EPCdeclare @prevtpc money -- current TPCdeclare @blntmp BIT --Cursor to hold all recordsDECLARE ClaimCursor CURSOR LOCAL FORWARD_ONLY FAST_FORWARD FORSELECT A.processdate, C.RMS_claim_id, B.i_level, A.IPC, A.EPC, A.TPCFROM FFSR_table AS A INNER join DPR_Table AS B ON A.DM_Key = B.DP_Key INNER JOIN DCR_Table AS C ON C.DM_Key = A.DM_KeyORDER BY C.RMS_claim_id, B.i_level,a.Processdate --Open Cursor OPEN ClaimCursor --Get First Record FETCH NEXT FROM ClaimCursor INTO @processdate,@RMS,@inslayer,@ipc,@xpc,@tpc SET @blntmp = 1 WHILE @@FETCH_STATUS = 0 BEGIN IF @blntmp=1 -- The First Record BEGIN -- Set the first Record SET @prevprocessdate = @processdate SET @prevRMS=@RMS SET @previnslayer = @inslayer SET @blntmp=0 SET @previpc =@ipc SET @prevxpc =@xpc SET @prevtpc =@tpc END ELSE BEGIN IF @prevRMS=@RMS BEGIN IF @previnslayer = @inslayer AND @previpc=@ipc AND @prevxpc=@xpc AND @prevtpc=@tpc BEGIN -- Execute SCENARIO 1 print 'Scenario 1' UPDATE FFSR_table SET IPC_Chg= @ipc, EPC_Chg= @xpc, TPC_Chg= @tpc FROM FFSR_table AS A INNER join DPR_Table AS B ON A.DM_Key = B.DP_Key INNER JOIN DCR_Table AS C ON C.DM_Key = A.DM_Key WHERE C.RMS_claim_id = @prevRMS AND B.i_level = @previnslayer AND A.processdate = @prevprocessdate END ELSE BEGIN UPDATE FFSR_table SET IPC_Chg= 0, EPC_Chg= 0, TPC_Chg= 0 FROM FFSR_table AS A INNER join DPR_Table AS B ON A.DM_Key = B.DP_Key INNER JOIN DCR_Table AS C ON C.DM_Key = A.DM_Key WHERE C.RMS_claim_id = @prevRMS AND B.i_level = @previnslayer AND A.processdate = @prevprocessdate END -- Previous inslayer <> current inslayer IF @previnslayer <> @inslayer AND @prevRMS = @RMS BEGIN print 'Scenario 2' UPDATE FFSR_table SET IPC= @previpc- @ipc, EPC= @prevxpc-@xpc, TPC= @prevtpc-@tpc FROM FFSR_table AS A INNER join DPR_Table AS B ON A.DM_Key = B.DP_Key INNER JOIN DCR_Table AS C ON C.DM_Key = A.DM_Key WHERE C.RMS_claim_id = @RMS AND B.i_level = @inslayer AND A.processdate = @processdate END --SET Prev = Current Record SET @prevprocessdate = @processdate SET @previnslayer = @inslayer SET @prevRMS = @RMS SET @previpc =@ipc SET @prevxpc =@xpc SET @prevtpc =@tpc END ELSE -- Next RMS BEGIN --Set Previous = Current Record SET @prevprocessdate = @processdate SET @previnslayer = @inslayer SET @prevRMS=@RMS SET @blntmp=0 SET @previpc =@ipc SET @prevxpc =@xpc SET @prevtpc =@tpc END END --Get Next Record FETCH NEXT FROM ClaimCursor INTO @processdate,@RMS,@inslayer,@ipc,@xpc,@tpc END CLOSE ClaimCursor DEALLOCATE ClaimCursorPlease advice. Thanks. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-10-11 : 14:47:26
|
| can you explain your requirement with some sample data rather than posting code? that will be much helpful to somebody trying to help you out. see link below for format of posting datahttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
 |
|
|
|
|
|
|
|