Solved

Delete Nodes in an Hierarchy

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

828 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