Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete with Sub-query "Query Cancelled by User"

Status
Not open for further replies.

putts

Programmer
Dec 13, 2002
93
US
I have a script with a bunch of statements and one of them is this.....

DELETE FROM TEMP_FII A WHERE WS_INSTANCE_ID IN (SELECT WS_INSTANCE_ID FROM TEMP_FII WHERE A.ID <> ID)

Clearly, what I'm trying to do here is insure that there WS_INSTANCE_ID is distinct within this table and I'm using that query to delete any duplicates.

It all works fine except in the case where there are no duplicates at which point I get the "Query Cancelled by User" message (from Query Tool ODBC). That would be all fine and dandy if it would just continue running the rest of the script but it bombs right there and wont finish.

Any ideas as to how to either:
1. Re-write that query so that it doesn't die if there are no duplicates.
2. Put some command in the script so that it will keep running even if this command dies.

I tried using an IF EXISTS but kept getting a syntax error so I'm thinking I can't do that (at least on our server or with the software/connection type that I'm using)
 
Do you care which duplicates get deleted? If not the following might work.

Code:
DELETE FROM TEMP_FII A 
  WHERE EXISTS (
    SELECT 1 FROM TEMP_FII B 
    WHERE A.WS_INSTANCE_ID = B.WS_INSTANCE_ID
      AND B.ID > A.ID)

This should keep the max id for each occurance of WS_INSTANCE_ID, and delete the rest.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top