Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > deleting from one table based on rows in another

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-20-03, 10:03
caf78 caf78 is offline
Registered User
 
Join Date: Mar 2003
Location: Denmark
Posts: 15
deleting from one table based on rows in another

I have 2 tables: OrderHeaders and OrderLines which has a one-to-many relationship.
Depending on if the field CreateDate in OrderHeaders has expired som date, I want to delete the corresponding rows in OrderLines AND OrderHeaders... but how?

The tables share the fields CompanyID, CustomerID, OrderNO.

Last edited by caf78 : 06-20-03 at 10:07.
Reply With Quote
  #2 (permalink)  
Old 06-20-03, 11:59
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 1,952
If OrderLines table was created with a foreign key constraint on OrderHeader with 'ON DELETE CASCADE' option, alll you have to do is:

Delete From OrderHeader
Where CreateDate <= ExpireDate;

Else, you need first to remove the OrderLines:

Delete From OrderLines L
Where Exists (
Select 1 From OrderHeader H
Where H.CompanyID = L.CompanyID
And H.CustomerID = L.CustomerID
And H.OrderNO = L.OrderNO
And L.CreateDate <= ExpireDate);

And then execute the delete of OrderHeader.
Reply With Quote
  #3 (permalink)  
Old 06-20-03, 12:21
dbmadcap dbmadcap is offline
Registered User
 
Join Date: May 2003
Posts: 87
Re: deleting from one table based on rows in another

You can have a daily process (procedure/function) scheduled to run at night to check for any such orders and then delete from OrderLines first and then from OrderHeaders.

Or you could use DBMS_JOB to schedule the procedure/function to do this. Once you have written the correct procedure/function to do this, it is easy to schedule using DBMS_JOB.

Say if you have a procedure 'test_job', you can schedule it as below :
Code:
declare l_job number; begin dbms_job.submit(l_job, 'test_job;',sysdate+1/200); end; / commit /

Hope this helps !!

Quote:
Originally posted by caf78
I have 2 tables: OrderHeaders and OrderLines which has a one-to-many relationship.
Depending on if the field CreateDate in OrderHeaders has expired som date, I want to delete the corresponding rows in OrderLines AND OrderHeaders... but how?

The tables share the fields CompanyID, CustomerID, OrderNO.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On