If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > SQL to Delete Records

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-21-07, 10:51
myBoo myBoo is offline
Registered User
 
Join Date: May 2007
Posts: 27
Question SQL to Delete Records

Getting an error with the following:

DELETE
FROM Table1 INNER JOIN Table2 ON Table1.FieldName = Table2.FieldName
Where (((Table2.FieldName) LIKE "criteria"));


Table 2 contains a primary key that ties the two tables together. It also contains another field which allows me to select specific record types.
It's a one-to-many with Table1.

Table 1 contains numeric data that is associated with Table2. No primary key.


Error Msg is: Specify the table containing the records you want to delete.

Can someone set me straight?
Reply With Quote
  #2 (permalink)  
Old 09-21-07, 11:16
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
DELETE FROM Table1 where Table1.FieldName in (select Table2.FieldName from table2 Where Table2.FieldName LIKE "criteria");

Andy
Reply With Quote
  #3 (permalink)  
Old 09-21-07, 16:40
myBoo myBoo is offline
Registered User
 
Join Date: May 2007
Posts: 27
Seems to work.

Thank you
Reply With Quote
  #4 (permalink)  
Old 09-22-07, 17:07
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by myBoo
DELETE
FROM Table1 INNER JOIN Table2 ON Table1.FieldName = Table2.FieldName
Where (((Table2.FieldName) LIKE "criteria"));
Joins (or views defined on joins) are never updatable, i.e., cannot be the subject of an insert, update or delete.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #5 (permalink)  
Old 09-23-07, 10:17
myBoo myBoo is offline
Registered User
 
Join Date: May 2007
Posts: 27
Quote:
Originally Posted by Peter.Vanroose
Joins (or views defined on joins) are never updatable, i.e., cannot be the subject of an insert, update or delete.
Thanks....

It seems the previous solution is not the best for my situation. I ran it and it seemed to work but after 24hrs only about 200 records were deleted with no end in sight. I had to force the command to end. Not very efficient.
Reply With Quote
  #6 (permalink)  
Old 09-23-07, 11:31
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
You might try a correlated subquery:

DELETE
FROM Table1
WHERE EXISTS (
SELECT * FROM Table2
WHERE Table1.FieldName = Table2.FieldName
AND Table2.FieldName LIKE "criteria") ;
Reply With Quote
  #7 (permalink)  
Old 09-25-07, 06:49
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,279
Slow deletes are very often due to FK constraints.

If you can, drop all FK constraints (pointing both to and from the table in question) and recreate the FK's after the delete.
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages
Reply With Quote
  #8 (permalink)  
Old 09-25-07, 07:29
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Dropping FK's from this table to others is not necessary. DB2 does not have to check those constraints in case of a DELETE operation.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #9 (permalink)  
Old 09-25-07, 08:05
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,279
Quote:
Dropping FK's from this table to others is not necessary. DB2 does not have to check those constraints in case of a DELETE operation.
stolze, that would be logic, but I have found out it does influences delete speed dramatically.

So now I drop all FK's, both defined on the table itself as on other tables that reference the "delete" table. Perhaps the latest DB2 versions behave different, I use this method for years now.
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages
Reply With Quote
  #10 (permalink)  
Old 09-26-07, 12:13
myBoo myBoo is offline
Registered User
 
Join Date: May 2007
Posts: 27
Quote:
Originally Posted by stolze
Dropping FK's from this table to others is not necessary. DB2 does not have to check those constraints in case of a DELETE operation.
I followed the suggestion by ARWinner..... My target table has 5000 records in it.... I'm trying to delete 32 of them.... ran overnight without returning my cursor.

Will try removing FKs from this table to see what results that provides.
Reply With Quote
  #11 (permalink)  
Old 09-26-07, 15:04
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by Wim
If you can, drop all FK constraints (pointing both to and from the table in question) and recreate the FK's after the delete.
When putting back the FKs, your tablespace(s) will be put in check pending state, so don't forget to run CHECK DATA after that (at least, on z/OS; no idea what's the equivalent on other platforms)
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #12 (permalink)  
Old 09-26-07, 16:14
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
DB2 LUW checks the FKs right away - no need for CHECK DATA or - what a closer equivalent would be - the SET INTEGRITY statement.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #13 (permalink)  
Old 10-15-07, 04:39
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,279
Quote:
Originally Posted by myBoo
I followed the suggestion by ARWinner..... My target table has 5000 records in it.... I'm trying to delete 32 of them.... ran overnight without returning my cursor.

Will try removing FKs from this table to see what results that provides.
Wat are your results ?
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages
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

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