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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Recursive Store Procedure with declare Cursor

Author  Topic 

yhchan2005
Starting Member

26 Posts

Posted - 2007-11-12 : 04:16:16
i had posted the same question in SQL2005 forum and get the answer using CTE, but CTE is not available in SQL2000. Just to check any
solution for below problem ? Thanks in Advance.

Hi, i got below sp which will calling back itself ( recursive ) in order to generate the hierarchy. Inside sp, i need to use cursor to hold the
record for looping. The problem is when i execute this sp, SQL server will raise error telling me the cursor already exists or open. Because this
is a recursive sp, how can i declare the cursor to avoid error ? or any other way to do it ? Please Help.


ALTER proc [dbo].[cpGetHierarchyDetail] (@vcPath varchar(1000),
@vcCompanyCode varchar(10),
@vcInvNo varchar(50))


as

Declare @vcPath varchar(1000)
Declare @vcSourceNo varchar(50)
Declare @vcSourceCompanyCode varchar(10)
Declare @vcType varchar(1)
Declare @vcImpExpTrn varchar(1)

Set @vcPath = @vcPath
if @vcInvNo = '' return

-- Declare Cursor to hold the Source Record
-- In some cases, it might contain more than 1 record

Declare csSource Cursor FOR Select InvoiceNo, CompanyCode, Type, ImpExpTrn,
SourceNo, SourceCompanyCode
from hoCombineSource
Where CompanyCode = @vcCompanyCode
and InvoiceNo = @vcInvNo

Open csSource
Fetch Next From csSource Into @vcInvNo, @vcCompanyCode, @vcType, @vcImpExpTrn,
@vcSourceNo, @vcSourceCompanyCode
While @@Fetch_Status = 0 begin

Set @vcPath = @vcPath + '\' + @vcInvNo

Insert into #tTemp(Path, CompanyCode, Type, ImpExpTrn)
Values(@vcPath, @vcCompanyCode, @vcType, @vcImpExpTrn)

-- Call back the same SP to continue
exec dbo.cpGetHierarchyDetail @vcPath, @vcSourceCompanyCode, @vcSourceNo


Fetch Next From csSource Into @vcInvNo, @vcCompanyCode, @vcType, @vcImpExpTrn,
@vcSourceNo, @vcSourceCompanyCode
end
Close csSource
Deallocate csSource

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-11-12 : 06:52:10
you could probably solve this with a temp table into which you insert data in the top proc and then
when recursivly calling the proc read/write data from/into the temp table.
to understand the scoping of the temp tables see here:
http://weblogs.sqlteam.com/mladenp/archive/2006/11/03/17197.aspx

also this should be also helpfull:
http://www.sqlteam.com/article/more-trees-hierarchies-in-sql


_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-11-12 : 07:03:15
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=92454

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2007-11-12 : 09:11:29
Another good source of info is Books Online... lookup "Expanding Hierarchies"...

--Jeff Moden
Go to Top of Page
   

- Advertisement -