Master-detail-detail-detial with Cascade delete

Master-detail-detail-detial with Cascade delete

Post by Jack Ratiwanic » Fri, 20 Sep 1996 04:00:00



I am currently working with MASTER-DETAIL-DETAIL-DETAIL relationship
in Delphi.
        The problem I have is when I try to cascade-delete these
records in Paradox tables. If the four tables and the first master
record
has full relationship with its children (means every detail-index key
has its sub-detail records), then the cascade-deleting of the four table
is O.K. However, if the first master record DOESN'T HAVE a full
relationship with its children (means the sub-detail and sub-sub-detail
records are empty), then the current pointer of the current detial
record
doesn't want to move or point to the next record.

                                MASTER-RECORD
                                     |
                -----------------------------------------
                |                    |                  |
        DETAIL-RECORD 1        DETAIL-REC 2     DETAIL-REC 3
                                     |
                        -------------------------
                        |                       |
                SUB-DETAIL-REC 1          SUB-DETAIL-REC 2
                        |                       |
        ---------------------------             |      
        |               |         |             |
SUB-SUB-DETAIL REC1   REC2      REC3     SUB-SUB-DETAIL-REC 4            

PROBLEM--> My pointer stops at DETAIL-RECORD1 after the first          
cascade-delete.

**** Please, any body help ?????


****************** BELOW IS THE PORTION OF MY SOURCE CODE *************

procedure TForm1.Table1BeforeDelete(DataSet: TDataset);
begin
   with Table2 do begin
      Try
        Try
         REFRESH;
        { First; }
         DisableControls;
        { IF FindKey([Table1.Fields[0].AsString]) THEN }
             while {(Fields[1].Asstring = Table1.Fields[0].Asstring)
and} (not EOF) do
             begin
                Table3.FindKey([Table2.Fields[0].AsString]);
                Delete;
             end;
        Finally
         EnableControls;
        end
      except
         on E: EDataBaseError  do
         If MessageDLG(E.Message + ' Delete your next layers. Continue
?',mtInformation,[mbYes,mbNo],0)=mrYes then
             Table1.Delete
         else
           Cancel;
      end; {End Tryig}
   end; {end Table2}

end;

procedure TForm1.Table2BeforeDelete(DataSet: TDataset);
begin
    with Table3 do begin
      Try
         REFRESH;
         IF State <> dsEdit then Edit;
        { Post;}
         First;
        DisableControls;
        FindKey([Table2.Fields[0].AsString]);
           while (Fields[1].Asstring = Table2.Fields[0].Asstring) and
(not EOF) do
             Delete;
      Finally
         EnableControls;
      end;
   end;

end;

procedure TForm1.Table3BeforeDelete(DataSet: TDataset);
begin
     with Table4 do begin
       Try
         REFRESH;
         if State <> dsEdit then Edit;
        { Post;  }
         First;
         DisableControls;
      {   IF FindKey([Table3.Fields[0].AsString]) THEN  }
           while {(Fields[1].Asstring = Table3.Fields[0].Asstring) and}
(not EOF) do
             Delete;
       Finally
         EnableControls;
       end;
     end;
end;

procedure TForm1.Table1AfterDelete(DataSet: TDataset);
begin
     Table1.SetRangeStart;

end;

procedure TForm1.Table2AfterDelete(DataSet: TDataset);
begin
      Table2.Edit;
      Table2.UpdateRecord;
      Table2.Post;
      Table3.Edit;
      Table3.UpdateRecord;
      Table3.Post;

      tABLE3.nEXT;
      Table3.GotoKey;
      Table3.MoveBy(1);
      Table3.FindKey([Table2.Fields[0].AsString]);
end;

procedure TForm1.Table3AfterDelete(DataSet: TDataset);
begin
    {Table3.UpdateRecord; }
end;

--

     WwwwwwW
  g(   o 0    )g
--oOO--(_)---OOo-------------------------------------------------------
                   Jack Ratiwanich,
                   1529 Paradise Lane, Cocoa FL 32922, U.S.A
                   Telephone Home: (407) 631-8465  Fax:(407) 638-1010
                   Work: (407) 638-1511   Suncom: 8-364-1511    


----\ (---) /----------------------- -----------------------------------
     \_) (_/  "Bisous..."

 
 
 

Master-detail-detail-detial with Cascade delete

Post by Danijel Tkalc » Mon, 23 Sep 1996 04:00:00



>I am currently working with MASTER-DETAIL-DETAIL-DETAIL relationship
>in Delphi.
>    The problem I have is when I try to cascade-delete these
>records in Paradox tables. If the four tables and the first master
>record
>has full relationship with its children (means every detail-index key
>has its sub-detail records), then the cascade-deleting of the four table
>is O.K. However, if the first master record DOESN'T HAVE a full
>relationship with its children (means the sub-detail and sub-sub-detail
>records are empty), then the current pointer of the current detial
>record
>doesn't want to move or point to the next record.
>PROBLEM--> My pointer stops at DETAIL-RECORD1 after the first          
>cascade-delete.

If your tables are related via MasterSource - MasterFields, you don't have to
search for the records that are inside the branch which is to be deleted, you
just delete all visible records.

So, try writing your events like this:

procedure TForm1.Table1BeforeDelete(DataSet: TDataset);
        begin
        with Table2 do
                begin
                DisableControls;
                try
                        Refresh;
                        First;
                        while not EOF do Delete;
                finally
                        EnableControls;
                end;
        end;

procedure TForm1.Table2BeforeDelete(DataSet: TDataset);
        begin
        with Table3 do
                begin
                DisableControls;
                try
                        Refresh;
                        First;
                        while not EOF do Delete;
                finally
                        EnableControls;
                end;
        end;

procedure TForm1.Table3BeforeDelete(DataSet: TDataset);
        begin
        with Table4 do
                begin
                DisableControls;
                try
                        Refresh;
                        First;
                        while not EOF do Delete;
                finally
                        EnableControls;
                end;

You DON'T NEED the extra AfterDelete events !
In fact, the're probably the reason why your method
did not work (you moved around too much).

  Danijel Tkalcec,
  Croatia.

My point of view :)

  *************
  * " The real beauty of human being is defined
  *     in the first place by how much and in which
  *        way has it released itself from its ego. "
  ************************
      Albert Eistein,
      Mein Weltbild, Amsterdam:
      Querido Verlag, 1934.

 
 
 

1. Cascading Delete Trigger in Master-Detail Relationships

Two Questions:
1. Why doesn't the following trigger work?  Note that STUDENTS is the
master table and STUDCOUNSELORS and TESTS are detail tables.  All three
tables have a field called FSID.  In this case, there are NOT foreign keys
in the detail tables. When the STUDENT record is deleted, the detail
records from the detail tables are NOT deleted.

CREATE TRIGGER CASCADE_DELETES ON dbo.STUDENTS
FOR DELETE
AS
begin



  from deleted d

  delete from STUDCOUNSELORS

  delete from TESTS

end

2. What is the best way to implement a cascading delete in a Master-Detail
relationship when a foreign key DOES exist in the detail tables?  Trigger
or Stored Proc?

David Sweeney
Texas A&M University

2. Example code your looking for!

3. MASTER/DETAIL with Cascading Deletes???? Need Help

4. I am trying to locate the T-SQL debugger which comes with Query Analyzer

5. Master/Detail Cascading Deletes???

6. VB/Access Programmer/Implementer Needed in Santa Barbara, California

7. Selecting from Master/Detail w/ Detail as columns of Master

8. SMBServer 5.0

9. Updating master-detail-detail Recordsets problem

10. Master->Detail->Detail Query Too Slow

11. Inserting detail rows in master-detail configuration (VB4)

12. Different detail Index order within master-detail relationship

13. Sorting detail records in a master detail relation.