I have a table FolderXDoc:
CREATE TABLE [dbo].[FolderXDoc](
[fldid] [int] NOT NULL,
[Xorder] [int] NOT NULL,
[docid] [int] NOT NULL,
CONSTRAINT [FolderXDoc$pk] PRIMARY KEY CLUSTERED
(
[fldid] ASC,
[Xorder] ASC,
[docid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
My application allows cyclic references in this table, so the following data is allowed:
fldid|Xorder|docid
1|1|2
2|1|3
3|1|4
4|1|2
So folder 1 contains folder 2 which contains folder 3. Folder 3 contains folder 4. Folder 4 contains folder 2 so that we have a cycle (1/2/3/4/2/3/4/2/3/4/...)
Now I want to retrieve all contained elements of a folder recursively. I tried this with CTE, but because of the cycle of the data this does not work. I would like to stop the recursion when the loop is detected. So when I retrieve the contained elements of 1 I'd expect the result set (2,3,4).
I tried this with a user defined function:
CREATE FUNCTION [dbo].[DocChildren](@fldid int)
RETURNS TABLE
AS
RETURN
(
WITH n AS
(SELECT f.fldid, f.docid
FROM folderxdoc f where f.fldid = @fldid
UNION ALL
SELECT n.fldid, nplus1.docid
FROM folderxdoc as nplus1, n
WHERE n.docid = nplus1.fldid and n.docid != @fldid)
SELECT docid FROM n
)
The function handles a cyclic loop of the starting id, but not when the cycle occurs in a contained element. What can I do to solve this problem?
Thanks for your help!