简体   繁体   中英

DELETE DB2 using OPENQUERY returns error about key column insufficient

I am trying to delete rows on DB2 i Series using a link server but am getting an error message. Key column information is insufficient or incorrect. Too many rows were affected by update

This is the query

DELETE FROM DB2
FROM OPENQUERY(TEST1, 'SELECT FIELD1 FROM LIBRARY1.FILE1') DB2
INNER JOIN #DLT_FILE1 DLT ON 
DB2.FIELD1 = DLT.FIELD1

There is one column in both temp file #DLT_FILE1 and DB2 table LIBRARY1.FILE1

The error message suggests that the join condition between the temporary table (#DLT_FILE1) and the DB2 table (LIBRARY1.FILE1) is not specific enough and is returning too many rows.

It's possible that the join condition is incorrect, or that there are duplicate values in the FIELD1 column in one or both tables.

You can try to Check the data in both tables to ensure that the join condition is correct and that there are no duplicate values in the FIELD1 column, Add a unique constraint on the FIELD1 column in both tables and Use a subquery to limit the number of rows that are being deleted in the join statement.

Use the ROW NUMBER () function to rank the rows and delete only the top rank.

You can also try to use a 'WHERE EXISTS' clause.

DELETE FROM DB2 FROM OPENQUERY(TEST1, 'SELECT FIELD1 FROM LIBRARY1.FILE1') DB2 WHERE EXISTS (SELECT 1 FROM #DLT_FILE1 DLT WHERE DB2.FIELD1 = DLT.FIELD1)

Db2 for IBM i (aka DB2-400) doesn't allow positioned deletes, ie from a cursor, that uses joins.

AMarc's suggestion might work, once you fix the syntax...I believe this is correct.

DELETE 
 FROM OPENQUERY(TEST1
               , 'SELECT FIELD1 FROM LIBRARY1.FILE1 DB2
                  WHERE EXISTS (SELECT 1 
                                FROM #DLT_FILE1 DLT 
                                WHERE DB2.FIELD1 = DLT.FIELD1)
                 ')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM