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
 SQL Server Development (2000)
 Using Cursors/Loops in an Stored Procedure

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-03-14 : 23:59:07
Matt writes "I need to set up 2 cursors in a single stored procedure, with data from the first cursor being passed into the second cursor as the table in the from clause. Then I need to insert values returned by both cursors into a single row, then continue looping until all rows for all tables are processed. Here is what I have so far.

CREATE Procedure sp_ListID

As
declare @selectstr varchar(30)
declare @ID float(8)
declare @PDID float(8)

DECLARE c1 CURSOR FOR
SELECT idx , reverse(substring(reverse(persistentmetadatat.classNameX),1,charindex('.',reverse(persistentmetadatat.classNameX),1)-1)) + 'T'
FROM persistentmetadatat
WHERE reverse(substring(reverse(persistentmetadatat.classNameX),1,charindex('.',reverse(persistentmetadatat.classNameX),1)-1))!= 'SelLineItemBundle'

OPEN C1

FETCH NEXT FROM c1
into @ID, @selectstr

WHILE @@FETCH_STATUS = 0
BEGIN


DECLARE c2 CURSOR FOR
select idx from @selectstr

OPEN C2
FETCH NEXT FROM c2
into @PDID
WHILE @@FETCH_STATUS = 0
BEGIN

Insert into lkp_MDProduct
(MD_id , Product_id)
values (@ID, @PDID)

FETCH NEXT FROM c2
into @PDID

FETCH NEXT FROM c1
into @ID, @selectstr

END
CLOSE c1
CLOSE c2
DEALLOCATE c1
DEALLOCATE c2
"
   

- Advertisement -