Solved

How to delete a record using SQL

Posted on 2004-08-12
5
172 Views
Last Modified: 2010-04-05
Hi, I have a question

I have two tables A and B. I want to delete a record in A and all the record with the same ArtikelID in B. How can i do this by using SQL ?
Can You answer ASAP

thx
0
Comment
Question by:marioway
5 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 11783956
DELETE
FROM A
WHERE A.ID IN (SELECT B.ID FROM B)
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 11784030
Now I red carefully your question... My query delete only records from A that have records with the same ID in B.

You will need to use stored procedure to achieve what you want.
0
 
LVL 17

Expert Comment

by:mokule
ID: 11784039
From my appliation


      sid := DM1.atSubst.FieldByName('sub_id').AsInteger;
      DM1.ADOConnection1.BeginTrans;
      try
        DM1.ADOCommand1.CommandText :=
          Format('DELETE FROM SubstNames WHERE nam_subst_id=%d', [sid]);
        DM1.ADOCommand1.Execute;

        DM1.ADOCommand1.CommandText :=
          Format('DELETE FROM Substances WHERE sub_id=%d',[sid]);
        DM1.ADOCommand1.Execute;

        DM1.ADOConnection1.CommitTrans;
      except
        DM1.ADOConnection1.RollbackTrans;
        end;
0
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 500 total points
ID: 11785175
You need to execute two queries.

DELETE from B where ArtikelID = :ArtikelID;
DELETE from A where ArtikelID = :ArtikelID;

I've given parameterized queries here as example. Now, it could be possible that ADO can work with multi-query commands so you could put this in a single ADOQuery component. But in general it's better to put both queries in their own ADOQuery components.

Now, some databases can maintain relationships between tables for you and offer the option of cascading deletes. This means that if you delete a record from table A, all related records from B (and C, D, E and F) will be deleted too. But again, this depends upon how the database is set up. (How you created it!)

I'm not that good as a database expert so someone might help you with that. But cascading deletes might be useful for you...
0
 

Author Comment

by:marioway
ID: 11792883
Thx
a lot

Marioway
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

777 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