I have a table like the following,
Create Table Entries
(
EntryID int,
ParentEntryID int, // -1 if root node
EntryText nvarchar(100)
)
I need to write a stored procedure which takes a leaf EntryID
and returns the all entries through the root entry starts from that leaf entry. What is the best way of doing it?
My desired stored procedure would look something like the following,
CREATE PROCEDURE dbo.GetPath
@leafEntryID int
AS
// what to do...
GO
I searched for it, and found two ways:
- Using a temporary table and basically using a
for
loop insert new rows to it. (I'm not sure how exactly implement it.) - Using cursors. (I don't have much idea about this way, is it a better approach?)
PS: I'm using Microsoft SQL server 2008