views:

101

answers:

7
+2  Q: 

DELETE and ACCESS

Where is the error in this statement?

query = "DELETE TOP 10 FROM table WHERE field LIKE \"something*\""

I get an error on the query sytax.

Thanks.

A: 

pay attention on quotation marks LIKE 'something*'

Arseny
tried, but nothing.
Alex
+3  A: 

You can't use TOP with DELETE. You must identify the rows, then delete them.

David M
since it's a big database (2 GB), if i delete the rows without limitating them after some minutes I get an error "resources insufficient" (i have to delete about 150k rows). Any idea to skip this?
Alex
if your Access DB is 2 gigs, seriously consider using a 'real' database instead. Access really isn't very scalable.
mgroves
I second the recommendation that a database with larger capacity is appropriate, but dispute the assertion that Jet/ACE is not a "real" database. Everybody always says that but nobody can ever identify what it is that makes it true.
David-W-Fenton
+1  A: 

Try

query = "DELETE * from (Select TOP 10  * FROM table WHERE field LIKE \"something*\")"

While you can't directly use top with Delete, you can use it for a derived table, and then Delete from the derived table.

cmsjr
i still get insufficient resources error...any idea?
Alex
Try running just the subquery Select TOP 10 * FROM table WHERE field LIKE \"something*\" to see if you can get any results back. Let me know if it errors out at that point, or if it is the delete.
cmsjr
A: 

This may or may not be possible depending on what database you're using, but you may be able to write your query something like this:

DELETE FROM TBLWHATEVER WHERE FLDWHATEVER LIKE 'something%' AND ROWNUM < 10

It depends on whether your DB has a query function like ROWNUM.

MusiGenesis
Whether or not Jet/ACE has a function ROWNUM or something like it is, of course, something that you personally couldn't have investigated before blithely suggesting it.
David-W-Fenton
Oh god, *you* again. I apologize for disparaging your *precious* once again. Thanks for reminding me to put `ms-access` in my excluded tags list.
MusiGenesis
You managed to post without disparaging Access, but you did give an answer that was wrong because you couldn't be bothered to check if your brilliant solution was applicable to the question being asked. If I gave an Access-specific answer to an Oracle question, it would be perfectly correct for others to ridicule me for not bothering to figure out if the answer applied or not.
David-W-Fenton
Actually, David, it would *not* be perfectly correct for others to ridicule you, especially if you prefaced your comment with something like, I don't know, maybe "this may or may not be possible depending on what database you're using". If you gave an Access-specific answer to an Oracle question, I would simply assume that you (like me here) just didn't notice the tag, and I would leave you alone.
MusiGenesis
It *would*, however, be perfectly correct for others to riducule you for continuing to use Access in the first place.
MusiGenesis
@MusiGenesis: you're such a model of maturity. Why don't you just delete your useless post and be done with it?
David-W-Fenton
A: 

If you can't do all the deletes at once, and moving to a more scalable database isn't an option, then you might try partitioning the data based on some other field and doing multiple deletes.

Let's assume you have a field called name and it's distribution is roughly even throughout the alphabet. You could do multiple deletes like this:

query0 = "DELETE FROM table WHERE field LIKE \"something*\" and name <= \"D\""
query1 = "DELETE FROM table WHERE field LIKE \"something*\" and name <= \"H\""
query2 = "DELETE FROM table WHERE field LIKE \"something*\" and name <= \"L\""
query3 = "DELETE FROM table WHERE field LIKE \"something*\" and name <= \"P\""
query4 = "DELETE FROM table WHERE field LIKE \"something*\" and name <= \"T\""
query5 = "DELETE FROM table WHERE field LIKE \"something*\" and name <= \"X\""
query6 = "DELETE FROM table WHERE field LIKE \"something*\"
Don Kirkby
A: 

You could make this work using a nested query like this...


DELETE FROM [TABLE] WHERE [Col1] = (
SELECT TOP 10 [Col1] FROM [TABLE] WHERE [criteria] ORDER BY [criteria]
);

Note in the subquery you're simply feeding a list into the main delete query by specifying what you want to delete from Col1, and the more criteria you have the fewer resources you'll need because more options for deletion will be eliminated.

To test this first Omit the DELETE FROM portion of the syntax and just run the query so you can see what you'll be feeding into your DELETE statement like this...

SELECT TOP 10 [Col1] FROM [TABLE] WHERE [criteria] ORDER BY [criteria]

Its important that you use the ORDER BY clause in case your sub query returns more than 10 results, that way you have a higher degree of control over what you're deleting.

Lucretius
+1  A: 

Try

Delete * from [tablename] where ID in (Select Top 10 ID from [Tablename] Where [Field] Like '*Condition*'

This way, you aren't looking up * (everything) in * (everything).

rrrhys