Solved

Delete Nodes in an Hierarchy

Posted on 2006-06-27
5
315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 28

Accepted Solution

by:
imran_fast earned 500 total points
ID: 16992060
/*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
ID: 16998517
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
ID: 16998754
>>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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

732 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