Preorder Tree Traversal (left/right tree) Deleting a Node MSSQL2005
Database August 17th, 2008This Stored Procedure deletes a node in a left/right tree (Preorder Tree Traversal) and all its children. The two tables are what you need to understand the Stored Procedure.
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 | /****** Object: StoredProcedure [dbo].[sp_DeleteTreeStructure] Script Date: 08/17/2008 19:12:16 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo].[sp_DeleteTreeStructure] ( @TreeStructureId INT -- Id TreeStructure to be deleted ) AS BEGIN SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; DECLARE @TranName NVARCHAR(MAX) SELECT @TranName = 'DeleteTreeStructure'; BEGIN TRANSACTION @TranName BEGIN TRY DECLARE @RightValue INT DECLARE @LeftValue INT DECLARE @Width INT DECLARE @TreeId INT SELECT @RightValue = RightValue, @LeftValue = LeftValue, @Width = (RightValue - LeftValue) + 1, @TreeId = TreeId FROM TreeStructures WHERE (TreeStructureId = @TreeStructureId) DELETE FROM TreeStructures WHERE (LeftValue BETWEEN @LeftValue AND @RightValue) AND TreeId = @TreeId UPDATE TreeStructures SET RightValue = RightValue - @Width WHERE RightValue > @RightValue AND TreeId = @TreeId UPDATE TreeStructures SET LeftValue = LeftValue - @Width WHERE LeftValue > @RightValue AND TreeId = @TreeId COMMIT TRANSACTION @TranName 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 in sp_DeleteTreeStructure. Rollback has been executed.' /**********************************************************/ END CATCH END |
No TweetBacks yet. (Be the first to Tweet this post)


Follow Me!