Hello, it's been a while since my last post, hope everyone is fine :P
I'm stuck with what seems to be a simple task, but I'm getting confused on how to complete this. Basically, I have been given a stored procedure which nests itself within itself and uses a temporary table to store the data while writing.
The nested stored procedure is used so that it can output the data rows in a db table into an organized tree like
Example 1
1.0
__1.1
__1.2
2.0
__2.1
__2.2
__2.3
etc...
Problem is, the nested stored proc is outputting the data in REVERSE order at the moment,
Example 2
2.0
__2.3
__2.2
__2.1
1.0
__1.2
__1.1
The table design is a the simple TreeView design concept, with a main ID column and the ParentID column representing the ID of it's parent.
The Stored Procedure will be used as part of a Forum Control, therefore ArticleId defining the Post that the user has selected to view.
I include the SQL Script for creating the Stored Proc below.
Can anyone please show me how to manipulate this so that the stored procedure outputs the data in the correct order as in Example 1 please
Just as a note, when executing this stored proc, we pass a 0 value to Root, e.g.
Many thanks
Davinski
I'm stuck with what seems to be a simple task, but I'm getting confused on how to complete this. Basically, I have been given a stored procedure which nests itself within itself and uses a temporary table to store the data while writing.
The nested stored procedure is used so that it can output the data rows in a db table into an organized tree like
Example 1
1.0
__1.1
__1.2
2.0
__2.1
__2.2
__2.3
etc...
Problem is, the nested stored proc is outputting the data in REVERSE order at the moment,
Example 2
2.0
__2.3
__2.2
__2.1
1.0
__1.2
__1.1
The table design is a the simple TreeView design concept, with a main ID column and the ParentID column representing the ID of it's parent.
The Stored Procedure will be used as part of a Forum Control, therefore ArticleId defining the Post that the user has selected to view.
I include the SQL Script for creating the Stored Proc below.
Code:
CREATE PROC dbo.ShowHierarchyForum ( @Root int, @ArticleId int ) AS BEGIN if not exists (select name from [tempdb].[dbo].[sysobjects] where name like '#YourLocalTempTable%') create table #YourLocalTempTable (Id int, ParentId int, ArticleId int) SET NOCOUNT ON DECLARE @CID int, @PID int insert into #YourLocalTempTable SELECT CP_FORUM_Comments.Id , ParentId ,ArticleId from CP_FORUM_Comments WHERE ID = @Root and ArticleId = @ArticleId SET @CID = (SELECT MAX(ID) FROM CP_FORUM_Comments WHERE ParentID = @Root) WHILE @CID IS NOT NULL BEGIN EXEC dbo.ShowHierarchyForum @CID, @ArticleId SET @CID = (SELECT MAX(ID) FROM CP_FORUM_Comments WHERE ParentID = @Root AND ID < @CID and ArticleId = @ArticleId) END END if @@NESTLEVEL =1 select * from #YourLocalTempTable GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO
Just as a note, when executing this stored proc, we pass a 0 value to Root, e.g.
Code:
exec ShowHierarchyForum 0, 1
Davinski
Comment