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)
 How to get rid of cursor

Author  Topic 

jronquil
Starting Member

2 Posts

Posted - 2006-04-04 : 16:18:49
Hi,

I implemented a udf with the folllowing code. I want to try and increase performance by eliminating the use of a cursor. Does anyone see a possible way to do this? Thanks in advance.

DECLARE @ProjectID int, @ParentWB nvarchar(50), @FullWB nvarchar(50)
DECLARE cur CURSOR
FOR SELECT ProjectID, ParentWB, FullWB FROM
FOR UPDATE of ParentID, ManagerID, IsActive

OPEN cur
FETCH NEXT FROM cur INTO @ProjectID, @ParentWB, @FullWB

UPDATE @PROJECT
set ParentID = Select ProjectID from @Project where FullWB = @ParentWB)

WHILE (@@fetch_status <> -1)
BEGIN
IF (@@fetch_status <> -2)
BEGIN

-- update ParentID
update @Project
-- update ParentID
set ParentID = (Select ProjectID from @Project where FullWB = @ParentWB),
-- update ManagerID
ManagerID = (select ManagerID from Project where ProjectID = @ProjectID),
-- update isProject
IsProject = ~(select convert(bit,count(ProjectID)) from @Project where ParentWB = @FullWB)
where ProjectID = @ProjectID
END
FETCH NEXT FROM cur INTO @ProjectID, @ParentWB, @FullWB

END

CLOSE cur
DEALLOCATE cur

jronquil
Starting Member

2 Posts

Posted - 2006-04-04 : 16:21:07
I forgot to mention that @Project is a temporary table i declared and populated with values.

quote:
Originally posted by jronquil

Hi,

I implemented a udf with the folllowing code. I want to try and increase performance by eliminating the use of a cursor. Does anyone see a possible way to do this? Thanks in advance.

DECLARE @ProjectID int, @ParentWB nvarchar(50), @FullWB nvarchar(50)
DECLARE cur CURSOR
FOR SELECT ProjectID, ParentWB, FullWB FROM
FOR UPDATE of ParentID, ManagerID, IsActive

OPEN cur
FETCH NEXT FROM cur INTO @ProjectID, @ParentWB, @FullWB

UPDATE @PROJECT
set ParentID = Select ProjectID from @Project where FullWB = @ParentWB)

WHILE (@@fetch_status <> -1)
BEGIN
IF (@@fetch_status <> -2)
BEGIN

-- update ParentID
update @Project
-- update ParentID
set ParentID = (Select ProjectID from @Project where FullWB = @ParentWB),
-- update ManagerID
ManagerID = (select ManagerID from Project where ProjectID = @ProjectID),
-- update isProject
IsProject = ~(select convert(bit,count(ProjectID)) from @Project where ParentWB = @FullWB)
where ProjectID = @ProjectID
END
FETCH NEXT FROM cur INTO @ProjectID, @ParentWB, @FullWB

END

CLOSE cur
DEALLOCATE cur

Go to Top of Page
   

- Advertisement -