Next Stored Procedure in MSSQL2005 inserts a Node in a left-right tree (Preorder Tree Traversal). It gives back the id of the inserted Node. The two tables are what you need to understand the Stored Procedure. The Stored Procedure uses one Function (you can easily copy paste the code from the function into the SP).

treestructures Preorder Tree Traversal (left/right tree) Inserting Node by index MSSQL2005

trees Preorder Tree Traversal (left/right tree) Inserting Node by index MSSQL2005

sp_inserttreenodeandreturntreestructureid:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/****** 
    Object:  StoredProcedure [dbo].[sp_InsertTreeNodeAndReturnTreeStructureId]    
    Script Date: 08/16/2008 09:29:07 
******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_InsertTreeNodeAndReturnTreeStructureId]
	(
		@TreeStructureId INT,    -- Parent Node
		@TreeId INT,                -- TreeId (in case no TreeStructures exist yet for the Tree)
		@INDEX INT                  -- Index where to be inserted (if null, insert at last place)
	)
AS
BEGIN
	SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
	DECLARE @TranName NVARCHAR(MAX)
	SELECT @TranName = 'InsertTreeNodeAndReturnTreeStructureId';
	BEGIN TRANSACTION @TranName
	BEGIN TRY
 
		DECLARE @RightValue INT
		DECLARE @TargetTreeStructureId INT
		IF (@TreeStructureId IS NULL)
		   -- Tree exists but no TreeStructures yet for Tree 
		    BEGIN
		        SET @RightValue = 1
		    END
		ELSE
		    BEGIN
			IF(@TreeId IS NULL)
			    -- With TreeStructureId it's easy to get the TreeId
			    BEGIN
			        SELECT     @TreeId = TreeId 
			        FROM         TreeStructures 
			        WHERE       TreeStructureId = @TreeStructureId
			    END
			IF (@INDEX IS NULL OR @INDEX = -1)
			    BEGIN
			        -- Node will be inserted at last place
			        SELECT	@RightValue = RightValue
			        FROM	        TreeStructures
			        WHERE	TreeStructureId = @TreeStructureId
			    END
			ELSE
			    BEGIN
			        SET @TargetTreeStructureId = 
                                    dbo.[fn_Edumatic3_GetChildTreeStructure](@TreeStructureId, @INDEX)		
			        IF(@TargetTreeStructureId <> -1)
				    BEGIN
				        SELECT  @RightValue = LeftValue
				        FROM	    TreeStructures
				        WHERE   TreeStructureId = dbo.[fn_GetChildTreeStructure](@TreeStructureId, @INDEX)
				    END
					ELSE
					    BEGIN
					        SELECT	@RightValue = RightValue
					        FROM  	TreeStructures
					        WHERE	TreeStructureId = @TreeStructureId
					    END
			    END
 
			-- Move TreeStructure row
			UPDATE  TreeStructures 
			SET       RightValue = RightValue + 2 
			WHERE   RightValue > (@RightValue - 1) 
			AND       TreeId = @TreeId
 
			UPDATE  TreeStructures 
			SET       LeftValue = LeftValue + 2 
			WHERE   LeftValue > (@RightValue - 1) 
			AND       TreeId = @TreeId
		   END
 
	       -- Insert new TreeStructure row
	       INSERT 
	       INTO     TreeStructures (TreeId, LeftValue, RightValue) 
	       VALUES  (@TreeId, @RightValue, @RightValue + 1)
 
	       COMMIT TRANSACTION @TranName
 
	       RETURN @TreeStructureId
	END TRY
 
	BEGIN CATCH
 
		/* If an error occured we Roll Back the transaction and 
                    raise an error that VS can catch */
 
		ROLLBACK TRANSACTION @TranName
		RAISERROR 50001 'Error sp_InsertTreeNodeAndReturnTreeStructureId. 
                                         Rollback has been executed.'
 
		/*************************************************************/	
 
	END CATCH
 
END

fn_edumatic3_getchildtreestructure:
Gets the id of the child node at a give index of a given parent node.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/****** 
    Object:  UserDefinedFunction [dbo].[fn_GetChildTreeStructure]    
    Script Date: 08/16/2008 14:03:45 
******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[fn_Edumatic3_GetChildTreeStructure] 
(
	@ParentTreeStructureId INT,
	@INDEX INT
)
RETURNS INT
AS
BEGIN
	DECLARE @TreeStructureId INT
	DECLARE @RightValue INT
	DECLARE @RootLeftValue INT
	DECLARE @RootRightValue INT
	DECLARE @TreeId INT
 
	SELECT	@RootLeftValue = LeftValue, 
			@RootRightValue = RightValue, 
			@TreeId = TreeId
	FROM 	TreeStructures
	WHERE	TreeStructureId = @ParentTreeStructureId
 
	SELECT	@RightValue = RightValue, 
			@TreeStructureId = TreeStructureId 
	FROM 	TreeStructures 
	WHERE	TreeId = @TreeId AND 
			LeftValue = @RootLeftValue + 1
 
	WHILE (@RightValue < (@RootRightValue - 1) AND @INDEX > 0) 
		BEGIN
			SELECT	@RightValue = RightValue, 
					@TreeStructureId = TreeStructureId 
			FROM 	TreeStructures 
			WHERE	TreeId = @TreeId AND 
					LeftValue = @RightValue + 1	
 
			SET   	@INDEX = @INDEX -1	
		END
 
	IF @INDEX <> 0
		-- Either negative index was passed or index was bigger than number of childs
		BEGIN
			SET @TreeStructureId = -1
		END
 
	RETURN @TreeStructureId
 
END
pixel Preorder Tree Traversal (left/right tree) Inserting Node by index MSSQL2005
No TweetBacks yet. (Be the first to Tweet this post)