[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.2

How do cascade delete the children of a parent down the generations?

Asked by DILORENZO in Operating Systems Miscellaneous

Tags: cascade, delete, sql

Suppose I have a table with two columns,  id   and  parent.  If I want to delete an Id all the times that id shows as a parent need thier ids collected and the process repeated until all are deleted.  I have tried everything.  I really want the code to do the process.


<%
system2 = request.form("system2")
node2 = request.form("node2")
seperate = split(node2,",")
node = seperate (0)
two = seperate (1)


      Sub DeleteC(ID)
            Set DeleteC_Conn = Server.CreateObject("ADODB.Connection")
            DeleteC_Conn.Open ("TT")
            Set DeleteC_RS = Server.CreateObject("ADODB.Recordset")
            DeleteC_RS.Open "SELECT id FROM " & system2 & "  WHERE ID = " & node, DeleteC_Conn, 3, 3
            DeleteC_RS.Delete
            DeleteC_RS.Close
            Set DeleteC_RS = Nothing      
            DeleteC_Conn.Close
            Set DeleteC_Conn = Nothing
      End Sub

      '---------------------------------------
      ' Function:      ReturnList(ID)
      '           Returns a pipe(|) delimited
      '                  list of comments with ID as
      '                  their 'parent.'
      '---------------------------------------
      Function ReturnList(ID)
            On Error Resume Next
            set ReturnListConn = Server.CreateObject("ADODB.Recordset")
            ReturnListConn.ActiveConnection = "TT"
            ReturnListConn.Source = "SELECT ID, Parent FROM " & system2 & " WHERE Parent = " & ID
            ReturnListConn.Open
            If ReturnListConn.EOF Then ReturnList = 0
            ReturnList_Return = ""
                        
            While Not ReturnListConn.EOF
                  ReturnList_Return = ReturnList_Return & ReturnListConn("ID") & "|"      
                  ReturnListConn.MoveNext
            Wend
            
            ' Strip the last | from the end.
            ReturnList = Left(ReturnList_Return, Len(ReturnList_Return) - 1)
            
            ReturnListConn.Close
            Set ReturnListConn = Nothing
                        
      End Function
      
      '---------------------------------------
      ' Sub:            DoIt(ID)
      '           Removes anything related to
      '                  ID. This is the actual code.
      '---------------------------------------

      Sub DoIt(ID)
            tempHitList = ReturnList(ID)
            For parseloop = 0 to UBound(Split(tempHitList, "|"))
                  workingwith = Split(tempHitList, "|")(parseloop)
                  if workingwith <> "0" and workingwith <> "" then
                        For parseloop2 = 0 to UBound(Split(workingwith, "|"))
                              tmpHL = Split(workingwith, "|")(parseloop2)
                              DeleteC(tmpHL)
                              Call DoIt(tmpHL)
                        Next
                  end if
            Next
      End Sub
            
      '---------------------------------------
      ' Set Variables
      '---------------------------------------

      ' Target: The initial CommentID to remove.
      Target = Request.QueryString("ID")
      
      ' Delete the actual target record.
      Call DeleteC(Target)
      
      ' Search and recursive delete the rest.
      DoIt(Target)

%>





Or maybe something  simpler like




Dim conn, sql
    Set conn = Server.CreateObject("ADODB.Connection")
   'conn.open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.Mappath("db2.mdb") & ";"
    conn.open "TT"
            'creating a recordset
set rs = Server.CreateObject("ADODB.Recordset")      

sub ATOMIC
    sql = " Update " & system2 & " set id = 0 where id = " & ID
      rs.open sql, conn
      
      do While str = " select 0 from " & system2 & " where id = '0' and parent <> '0'" exisits
      rs.open str

                        stp = " update " & system2 & " set paretn = '0' where id = '0' and parent <> '0'"
                        rs.open stp
                  loop
                        
                        gol = " delete from " & system2 & " where id = 0"
                        rs.open gol
      end sub
call atomic
%>
</body>
</html>


or


Set conn = Server.CreateObject("ADODB.Connection")
   'conn.open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.Mappath("db2.mdb") & ";"
    conn.open "TT"
            'creating a recordset
set rs = Server.CreateObject("ADODB.Recordset")


stp = "delete from " & system2 & " where id = " & ID & ";"

      rs.open stp, conn
      
do

sql = " Select id from " & system2 & " where parent = " & id
str = "delete from " & system2 & " where id = " & id
set irs = Server.CreateObject("ADODB.Recordset")
      
      
      irs.open sql
      id = irs("id")
      response.write(id)
      irs.close
loop
      

or changing the table  like

system2 = request.form("system2")
node2 = request.form("node2")
hi = split(node2, ",")
ID = hi(0)

   Set conn = Server.CreateObject("ADODB.Connection")
   'conn.open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.Mappath("db2.mdb") & ";"
   conn.open "TT"
            'creating a recordset
      set rs = Server.CreateObject("ADODB.Recordset")      
sql = "ALTER DATABASE TT SET RECURSIVE_TRIGGERS ON GO CREATE TRIGGER trg_d_Tree ON" & system2 & "FOR DELETE AS IF @@rowcount = 0 RETURN DELETE FROM T FROM " & system2 & " AS parent JOIN deleted AS id ON parent = id GO"
str = "DELETE FROM " & system2 & " WHERE id = " & hi(0)

stp = "SELECT * FROM " & system2

rs.open  str, stp, conn

%>
</body>
</html>
   



but none of these attempts will work.  
[+][-]08/14/03 02:38 PM, ID: 9150359Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/15/03 01:43 PM, ID: 9162619Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: Operating Systems Miscellaneous
Tags: cascade, delete, sql
Sign Up Now!
Solution Provided By: K_2K
Participating Experts: 3
Solution Grade: B
 
[+][-]08/16/03 07:35 AM, ID: 9165580Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/18/03 09:36 AM, ID: 9174681Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/19/03 09:11 AM, ID: 9181852Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/21/03 11:17 AM, ID: 9197541Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81