I have many similar structure tables like this:
CREATE TABLE [dbo].[tbl_Hierarchy](
[ID] [int] NOT NULL,
[ParentID] [int] NOT NULL,
[Text] [nvarchar](100) NOT NULL,
--other field irrelevant to my question
)
INSERT INTO dbo.tbl_Hierarchy VALUES(1,0,'parent1')
INSERT INTO dbo.tbl_Hierarchy VALUES(2,0,'parent2')
INSERT INTO tbl_Hierarchy VALUES(3,1,'child1')
INSERT INTO tbl_Hierarchy VALUES(4,3,'grandchild1')
INSERT INTO tbl_Hierarchy VALUES(5,2,'child2')
Can you help me writing such as a stored procedure including two parameters with table name and ID ?
For example, when executing
EXEC usp_getChildbyID tbl_Hierarchy, 1
the result set should be:
ID Text Level
1 parent1 1
3 child1 2
4 grandchild1 3
Thanks a lot in advance.