Solved

How to delete a record using SQL

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month5 days, 15 hours left to enroll

627 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