Solved

Delete Nodes in an Hierarchy

Posted on 2006-06-27
5
312 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
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

911 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

21 Experts available now in Live!

Get 1:1 Help Now