Hello,
I have this query where I'm using a cursor. I was wondering if there is a way I could change it and NOT use a cursor. Any help would be greatly appreciated. Thanks in advance!
I have this query where I'm using a cursor. I was wondering if there is a way I could change it and NOT use a cursor. Any help would be greatly appreciated. Thanks in advance!
Code:
CREATE PROCEDURE dbo.usp_Path_Select
AS
DECLARE @Name varchar(500)
--TEMP TABLE
CREATE TABLE #tempID
(Uid int,path varchar(500))
INSERT INTO #tempID
SELECT DISTINCT
FH.UniqueID,
FH.[Name]
FROM FileHieararchy FH
INNER JOIN tbl_File_Views F_V ON F_V.UniqueID = FH.UniqueID
INNER JOIN tbl_File_ViewDetails F_VD ON F_VD.GroupID = F_V.GroupID
INNER JOIN tbl_File_Details F_D ON F_D.FileID = F_VD.FileID
WHERE F_V.GroupID IS not null
DECLARE idCursor CURSOR FOR
--iterating through the temp table and getting the full path based on the Uid
SELECT Uid FROM #tempID
FOR READ ONLY
DECLARE @tempUid int
OPEN idCursor
FETCH NEXT FROM idCursor INTO @tempUid
WHILE(@@fetch_status = 0)
BEGIN
--setting the full path
SET @Name = dbo.lookUp(@tempUid)
UPDATE #tempID
SET path = @name WHERE Uid = @tempUid
FETCH NEXT FROM idCursor INTO @tempUid
END CLOSE idCursor
DEALLOCATE idCursor
SELECT Uid AS UniqueID,
path AS sName
FROM #tempID
--dumping the temp table
DROP TABLE #tempID
GO