SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS OFF GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ntm_test_insert_node]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[ntm_test_insert_node] GO -- converts from node_name to node_id. CREATE PROCEDURE ntm_test_insert_node @TreeID uniqueidentifier ,@buddy_name varchar(255) = null ,@node_name varchar(255) ,@node_table_name varchar(255) = 'ntm_nodes' ,@insert_type varchar(255) = "AS_CHILD" ,@active bit = 1 ,@use_alphabetic_order bit = 0 ,@str_debug varchar(255) = "NODEBUG" as declare @errormessage_str varchar(255) DECLARE @InsertID uniqueidentifier DECLARE @BuddyID uniqueidentifier declare @sql_str nvarchar(1000) begin if @TreeID is null begin set @errormessage_str = 'Error - The TreeID can not be null.' goto errorhandler end if @buddy_name = @node_name begin set @errormessage_str = 'Error - The New Node and its Buddy Node must have different names.' goto errorhandler end -- If the NodeName already exists in the table, then we are probably creating a node with -- multiple parents. set @sql_str = N'select @InsertID = InternalNodeID from ' + @node_table_name + N' where TreeID = ''' + convert(nvarchar(50), @TreeID) + N''' and NodeName = ''' + @node_name + N'''' execute sp_executesql @sql_str, N'@InsertID uniqueidentifier out', @InsertID out set @sql_str = N'select @BuddyID = InternalNodeID from ' + @node_table_name + N' where TreeID = ''' + convert(nvarchar(50), @TreeID) + N''' and NodeName = ''' + @buddy_name + N'''' execute sp_executesql @sql_str, N'@BuddyID uniqueidentifier out', @BuddyID out exec ntm_insert_node @node_table_name = @node_table_name ,@TreeID = @TreeID ,@node_name = @node_name ,@InsertID = @InsertID ,@BuddyID = @BuddyID ,@insert_type = @insert_type ,@active = @active ,@use_alphabetic_order = @use_alphabetic_order ,@str_debug = @str_debug /* return the success status code */ return(1) ErrorHandler: Print '*** Error While Executing NTM_TEST_INSERT_NODE ***' Print @ErrorMessage_str /* return the error status code */ return(-1) end GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO