Solved

How to delete a record using SQL

Posted on 2004-08-12
5
175 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
select query - oracle 16 103
Firemonkey webbrowser scrollbars ? 1 72
Installshield for Embarcadero EX 10.1 Berlin 4 92
How to make Sign in, using Clientdataset? 1 43
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 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