Solved

Delete Nodes in an Hierarchy

Posted on 2006-06-27
5
311 Views
Last Modified: 2012-05-05
Hi,
I have a table with the following columns

id int
name varchar
parent id

this table is used to create a tree structure of any level deep. Now I want to create a procedure/function that gets the node id as a parameter and deletes all its children till the end. Under the passed node id there can be any level of nodes...sub nodes ...etc.

can anyone please point me to an article or procedure/function that does this.

Thanks in advance
Venki

0
Comment
Question by:tovvenki
  • 2
5 Comments
 
LVL 28

Accepted Solution

by:
imran_fast earned 500 total points
Comment Utility
/*you have to create one additional table
the session no is to identify the souce for calling the delete statement
*/
create   table deleterecord (row_num int identity(1,1),id int, session_no int)
go
/*
create the procedure and call this to delete the ids
*/
create procedure delete_all_rec @parentid int , @session_no int
as
begin


insert into deleterecord (id, session_no) values (@parentid, @session_no)

declare c cursor for select id from YourTable where parent_id = @parentid

open c
fetch next from c into @parentid
      while @@fetch_status = 0
            begin
                  if not exists (select null from deleterecord where id = @parentid and session_no = @session_no)
                  exec delete_all_rec @parentid  , @session_no
                  fetch next from c into @parentid
            end
close c
deallocate c
delete from YourTable where id in (select id from deleterecord where  session_no = @session_no)
end
go
0
 
LVL 21

Author Comment

by:tovvenki
Comment Utility
Hi,
Thnaks for the prompt reply, but can you give some additional information on the the session no column, how it gets its value, whether the application send this id or...??

Thanks
venki
0
 
LVL 28

Expert Comment

by:imran_fast
Comment Utility
>>session no column, how it gets its value,

Session No is to track the sessions  for example you have 3 users working on search so each user should have one session no. Now this session no could be user_name. and it shuold be passed by the application .
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 5 108
Microsoft SQL query 7 37
SQL 2012 and SQL 2014 always on 9 23
INSERT INTO SELECT JOIN THING 2 24
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now