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 |
BioMash
Starting Member
8 Posts |
Posted - 2005-12-29 : 22:39:53
|
I have the following stored proc which I was working with yesterday without any prblems, I made some changes and now even when it reaches the end of the cursor it seems to start again....it just keeps running and running:)some help would be appreciatedALTER PROCEDURE [dbo].[AA_pricelist_TEST] @intRound int = null, @decPercentage decimal(3,2) = null, @intProductID int = null, @intValueID int = null, @intOldValueID int = null, @intCurrencyID int = null, @intTaxID int = null, @fltTaxPercent float = null, @fltValueAmount float = null, @NewValueAmount smallmoney = nullASDECLARE c1 CURSOR FORSelect pv.intProductID,v.intValueID,v.intCurrencyID,v.intTaxID,v.fltTaxPercent,v.fltValueAmountFROM tblProductValue pv, tblValue vWHERE pv.intValueID = v.intValueIDOPEN c1FETCH NEXT FROM c1INTO @intProductID, @intOldValueID, @intCurrencyID, @intTaxID, @fltTaxPercent, @fltValueAmountWHILE @@FETCH_STATUS = 0BEGIN-- changed to allow passing in of percentage and whole number to round up to.SET @NewValueAmount = CEILING((@fltValueAmount + (@fltValueAmount * @decPercentage))/@intRound)*@intRoundEXEC sp_Value_Insert @intValueID OUTPUT, @intCurrencyID = @intCurrencyID, @intTaxID = @intTaxID, @fltTaxPercent = @fltTaxPercent, @fltValueAmount = @NewValueAmountEXEC sp_ProductValue_NewAssign @intOldValueID = @intOldValueID, @intProductID = @intProductID, @intValueID = @intValueID---- add fetch statementFETCH NEXT FROM c1INTO @intProductID, @intOldValueID, @intCurrencyID, @intTaxID, @fltTaxPercent, @fltValueAmount---ENDCLOSE c1DEALLOCATE c1 |
|
sachinsamuel
Constraint Violating Yak Guru
383 Posts |
Posted - 2005-12-29 : 23:10:22
|
Whats the total number of record returned by the query?Select pv.intProductID,v.intValueID,v.intCurrencyID,v.intTaxID,v.fltTaxPercent,v.fltValueAmountFROM tblProductValue pv, tblValue vWHERE pv.intValueID = v.intValueIDI think its taking time but not infinite. Also check the execution plan to see if there is proper indexes and they don't need updated statistics.RegardsSachinDon't sit back because of failure. It will come back to check if you still available. -- Binu |
|
|
shallu1_gupta
Constraint Violating Yak Guru
394 Posts |
Posted - 2005-12-29 : 23:34:20
|
Hi BioMashsince you are declaring a cursor on the tables and with in the loop you are again inserting and updating the table on which cursor is based hence it is going in infinite loop.modify the proc to execute the cursor on a tem table.ALTER PROCEDURE [dbo].[AA_pricelist_TEST] @intRound int = null, @decPercentage decimal(3,2) = null, @intProductID int = null, @intValueID int = null, @intOldValueID int = null, @intCurrencyID int = null, @intTaxID int = null, @fltTaxPercent float = null, @fltValueAmount float = null, @NewValueAmount smallmoney = nullASSelect pv.intProductID,v.intValueID,v.intCurrencyID,v.intTaxID,v.fltTaxPercent,v.fltValueAmountinto #tempTableFROM tblProductValue pv, tblValue vWHERE pv.intValueID = v.intValueIDDECLARE c1 CURSOR FORselect * from #tempTableOPEN c1FETCH NEXT FROM c1INTO @intProductID, @intOldValueID, @intCurrencyID, @intTaxID, @fltTaxPercent, @fltValueAmountWHILE @@FETCH_STATUS = 0BEGIN-- changed to allow passing in of percentage and whole number to round up to.SET @NewValueAmount = CEILING((@fltValueAmount + (@fltValueAmount * @decPercentage))/@intRound)*@intRoundEXEC sp_Value_Insert @intValueID OUTPUT, @intCurrencyID = @intCurrencyID, @intTaxID = @intTaxID, @fltTaxPercent = @fltTaxPercent, @fltValueAmount = @NewValueAmountEXEC sp_ProductValue_NewAssign @intOldValueID = @intOldValueID, @intProductID = @intProductID, @intValueID = @intValueID---- add fetch statementFETCH NEXT FROM c1INTO @intProductID, @intOldValueID, @intCurrencyID, @intTaxID, @fltTaxPercent, @fltValueAmount---ENDCLOSE c1DEALLOCATE c1 |
|
|
shallu1_gupta
Constraint Violating Yak Guru
394 Posts |
Posted - 2005-12-29 : 23:50:42
|
or you can use INSENSITIVE which does not allow committed deletes and updates made to the underlying tables in subsequent fetches. and rest remains sameDECLARE c1 INSENSITIVE CURSOR FOR |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2005-12-30 : 07:53:46
|
or even better ....try to stay away from cursors.post a meaningful description of what is happenning in the sub-procedures (sp_Value_Insert, sp_ProductValue_NewAssign) and there may be a better non-cursor solution to your problem.sample code, input data and matching results would help a lot. |
|
|
|
|
|
|
|