| |
|
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.
|
 |

09-21-07, 10:51
|
|
Registered User
|
|
Join Date: May 2007
Posts: 27
|
|
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?
|
|

09-21-07, 11:16
|
|
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
|
|

09-21-07, 16:40
|
|
Registered User
|
|
Join Date: May 2007
Posts: 27
|
|
|
|

09-22-07, 17:07
|
|
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/
|
|

09-23-07, 10:17
|
|
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.
|
|

09-23-07, 11:31
|
|
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") ;
|
|

09-25-07, 06:49
|
|
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
|
|

09-25-07, 07:29
|
|
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
|
|

09-25-07, 08:05
|
|
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
|
|

09-26-07, 12:13
|
|
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.
|
|

09-26-07, 15:04
|
|
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/
|
|

09-26-07, 16:14
|
|
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
|
|

10-15-07, 04:39
|
|
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
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|