Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Delete Nodes in an Hierarchy

Posted on 2006-06-27
5
Medium Priority
?
319 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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

610 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