Get parent folder ids by folder id(s)
This is typically something you do with a cursor:

DECLARE @tmpFolderId INT;
DECLARE @RelatedParentFolderIds TABLE (FolderID INT, ParentFolderID INT);

DECLARE folderCurs CURSOR LOCAL FAST_FORWARD FOR SELECT FolderID FROM #TmpDistinctFolderIds;
OPEN folderCurs
FETCH NEXT FROM folderCurs INTO @tmpFolderId
WHILE @@FETCH_STATUS = 0
BEGIN
    -- Get all possible hierarchy level parent folder ids
    INSERT INTO @RelatedParentFolderIds (FolderID, ParentFolderID)
    SELECT FolderID, ParentFolderID
    FROM [dbo].[fnGetAllParentFolderIds] (@tmpFolderId)

    FETCH NEXT FROM folderCurs INTO @tmpFolderId
END