Solved

How to delete a record using SQL

Posted on 2004-08-12
5
171 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

912 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now