I’m currently writing a procedure, I have a file system set out in syBase I want to find all the files that has the file I specify in it and then I want it to find all the files that had all those files in it and so on.
Right now my code works and returns all the files I want however I would like a much more elegant version as currently it’s a LOT of repeat code and does not cover all of the files anyway.
I have tried looping but I’m not having much luck.
INSERT #nodes
SELECT from_file
FROM file_map
WHERE to_file = @fileNode
insert #fileList
SELECT DISTINCT file
from #nodes
INSERT #nodes1
SELECT from_file
FROM file_map m
INNER JOIN #nodes n ON m.to_file = n.file
WHERE m.to_file LIKE '/%'
INSERT #fileList
SELECT DISTINCT file
FROM #nodes1
INSERT #nodes2
SELECT from_file
FROM file_map m
INNER JOIN #nodes1 n ON m.to_file = n.file
WHERE m.to_file LIKE '/%'
INSERT #fileList
SELECT DISTINCT file
FROM #nodes2
INSERT #nodes3
SELECT from_file
FROM file_map m
INNER JOIN #nodes2 n ON m.to_file = n.file
WHERE m.to_file LIKE '/%'
INSERT #fileList
SELECT DISTINCT file
FROM #nodes3
It’s quite a simple procedure I just can’t figure out a way of making this neater/faster.
This is just an example, the system could go like 30 files deep so I don’t want to create 30 temp tables.
Stored procedures should be able to handle recursion (I’m not familiar with Sybase though). I’m not entirely sure what your schema looks like, but you can use recursion to keep the code lean (sometimes at a cost to performance). You’d need to maintain a set of visited files so that you avoid infinitely looping.