It is generally better to do things in the database as a select
query rather than using cursors. I believe the following will make efficient use of an index on Paths(path)
:
select p.*
from Paths p
where not exists (select 1
from Paths p2
where p.path like p2.path + '%' and
p.path <> p2.path
);
(Unfortunately, I cannot test this today.)
By the way, your cursor expression is missing the from
clause and that might have to do with why it doesn't work.