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-04-30 : 09:19:46
|
| shahin mohaghegh writes "I have a SP that is called recursively.I use Cursor Inside it, the problem is that the name of the cursor is not changed each time the SP is called and this cause error when the SP is called for the second time.the Error message as is expected is CURSOR ALREADY EXIST.the simplest solution to the problem is to find a way to be able to change the name of the cursor each time the SP is called.Also I've done it with the following Trick:declare @CursorName varchar(20)declare @SqlString varchar(4000)declare @Level intset @Level = 0set @CursorName = 'TCURSOR' + @Levelset @SqlString = 'DECLARE ' + @CursorName + ' CURSOR FOR SELECT MasterCode, DetailCode, Qty, MasterName, DetailName, Price from ...'EXEC sp_executeSQL @SQLStringbut i can not do the same for :FETCH NEXT FROM Tcursor0 INTO @MasterCode, @DetailCode, @Qty, @MasterName, @DetailName, @PriceThanks." |
|
|
timmy
Master Smack Fu Yak Hacker
1242 Posts |
Posted - 2002-04-30 : 09:26:50
|
Declare the cursor as local, then it won't cause conflicts:DECLARE cursorname CURSOR LOCAL FOR SELECT ...... |
 |
|
|
AjarnMark
SQL Slashing Gunting Master
3246 Posts |
Posted - 2002-04-30 : 13:33:59
|
| Another solution is to do away with the cursor completely. This is one of our favorite pasttimes here at SQLTeam, so if you want us to help out, just post your code and what your goal is...We have been able to dramatically speed up many a process by eliminating the evil cursor. |
 |
|
|
shahin_mohaghegh
Starting Member
2 Posts |
Posted - 2002-05-01 : 04:04:31
|
| the LOCAL option for the cursor did not help.here is rthe complete case information.I'm waiting for the answer, thanxTables:PRD---Code intName varchar (20)Price intPRDREL------MasterCode intDetailCode intQty intSample Values for Tables:---------------------------PRD---CODE NAME PRICE---- ---- -----0 NULL 01 One 10002 Two 20003 Three 30004 Four 40005 Five 50006 Six 60007 Seven 7000PRDREL------MASTERCODE DETAILCODE QTY---- ---- ----1 3 21 2 14 5 24 1 25 2 22 6 16 7 1Views:------View1-----ALTER VIEW dbo.VIEW1ASSELECT dbo.prdRel.*, dbo.prd.Name AS MasterName, prd_1.Name AS DetailName, prd_1.PriceFROM dbo.prd INNER JOIN dbo.prdRel ON dbo.prd.Code = dbo.prdRel.MasterCode INNER JOIN dbo.prd prd_1 ON dbo.prdRel.DetailCode = prd_1.Codecalling code:-------------declare @sum intexec TotalPrice 1, 0, @Sum outputselect @sumThe TotalPrice SP:------------------SET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS OFF GOALTER PROCEDURE [TotalPrice](@Code int, @Level tinyint, @SumPrice int output) ASdeclare @cnt intdeclare @MasterCode intdeclare @DetailCode intdeclare @Qty intdeclare @MasterName varchar(20)declare @DetailName varchar(20)declare @Price intDeclare @SqlString nvarchar(1000)declare @CursorName nvarchar(20)declare @CodeName nvarchar(20)declare @TotalSum intset @CursorName = 'TCursor' + cast(@Level as nvarchar(2)) Set @CodeName = cast(@Code as nvarchar(20))set @SqlString = 'DECLARE ' + @CursorName + ' CURSOR FOR SELECT MasterCode, DetailCode, Qty, MasterName, DetailName, Price from View1 where MasterCode = ' + @Codename set @SqlString = @SqlString + N' open ' + @cursornameEXEC sp_executeSQL @SQLStringSELECT @cnt=count(*) from View1 where MasterCode = @codeif @cnt=0 set @SumPrice=0else begin set @SumPrice = 0 set @TotalSum = 0 /*--------------------*/---- Because i Could not use EXEC sp_executeSQL method-- if @CursorName = 'Tcursor0' FETCH NEXT FROM Tcursor0 INTO @MasterCode, @DetailCode, @Qty, @MasterName, @DetailName, @Price if @CursorName = 'Tcursor1' FETCH NEXT FROM Tcursor1 INTO @MasterCode, @DetailCode, @Qty, @MasterName, @DetailName, @Price if @CursorName = 'Tcursor2' FETCH NEXT FROM Tcursor2 INTO @MasterCode, @DetailCode, @Qty, @MasterName, @DetailName, @Price if @CursorName = 'Tcursor3' FETCH NEXT FROM Tcursor3 INTO @MasterCode, @DetailCode, @Qty, @MasterName, @DetailName, @Price if @CursorName = 'Tcursor4' FETCH NEXT FROM Tcursor4 INTO @MasterCode, @DetailCode, @Qty, @MasterName, @DetailName, @Price ------------------*/ WHILE @@FETCH_STATUS = 0 BEGIN SELECT @cnt = count(*) from View1 where MasterCode = @DetailCode if @cnt=0 begin set @SumPrice = @SumPrice + @Qty * @Price end else begin Set @Level = @Level+1 exec TotalPrice @DetailCode, @Level, @SumPrice output set @SumPrice = @Qty * @SumPrice set @SumPrice = @SumPrice + @TotalSum endif @CursorName = 'Tcursor0' FETCH NEXT FROM Tcursor0 INTO @MasterCode, @DetailCode, @Qty, @MasterName, @DetailName, @Priceif @CursorName = 'Tcursor1' FETCH NEXT FROM Tcursor1 INTO @MasterCode, @DetailCode, @Qty, @MasterName, @DetailName, @Priceif @CursorName = 'Tcursor2' FETCH NEXT FROM Tcursor2 INTO @MasterCode, @DetailCode, @Qty, @MasterName, @DetailName, @Priceif @CursorName = 'Tcursor3' FETCH NEXT FROM Tcursor3 INTO @MasterCode, @DetailCode, @Qty, @MasterName, @DetailName, @Priceif @CursorName = 'Tcursor4' FETCH NEXT FROM Tcursor4 INTO @MasterCode, @DetailCode, @Qty, @MasterName, @DetailName, @Priceset @TotalSum = @TotalSum + @SumPrice ENDendset @SqlString = 'CLOSE ' + @CursorNameset @SqlString = @SqlString + N' DEALLOCATE ' + @CursorName EXEC sp_executeSQL @SQLStringGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOEdited by - shahin_mohaghegh on 05/01/2002 04:06:03Edited by - shahin_mohaghegh on 05/01/2002 04:16:52 |
 |
|
|
shahin_mohaghegh
Starting Member
2 Posts |
Posted - 2002-05-04 : 08:30:37
|
| thanks to TIMMY for his guide.i found the solution for my problem.here it goes:Create PROCEDURE TotalPrice( @MasterID int ,@Sum int Output)ASdeclare @ItemID int declare @Name nvarchar(50)declare @Price intdeclare @Qty intdeclare @TEmp intDECLARE TCursor CURSOR LOCAL FOR SELECT dbo.PRD.*, dbo.PRDREL.QtyFROM dbo.PRD INNER JOIN dbo.PRDREL ON dbo.PRD.ItemID = dbo.PRDREL.DetailIDWHERE (dbo.PRDREL.MasterID = @MAsterid)open tcursorFETCH NEXT FROM TCursor INTO @ItemID, @Name, @Price, @Qtyset @Sum = 0set @TEmp = 0WHILE @@fetch_status = 0BEGIN--select @ItemID, @Name, @Price, @Qty exec TotalPrice @itemid, @Temp output set @Sum = @Sum + @Price * @Qty + @Temp FETCH NEXT FROM TCursor INTO @ItemID, @Name, @Price, @QtyENDCLOSE TCUrsorDEALLOCATE TCUrsor |
 |
|
|
|
|
|
|
|