Solved

How to delete a record using SQL

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 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

13 Experts available now in Live!

Get 1:1 Help Now