简体   繁体   English

DELETE DB2 使用 OPENQUERY 返回有关键列不足的错误

[英]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.我正在尝试使用链接服务器删除 DB2 i 系列上的行,但收到错误消息。 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临时文件 #DLT_FILE1 和 DB2 表 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.错误消息表明临时表 (#DLT_FILE1) 和 DB2 表 (LIBRARY1.FILE1) 之间的连接条件不够具体,返回的行太多。

It's possible that the join condition is incorrect, or that there are duplicate values in the FIELD1 column in one or both tables.连接条件可能不正确,或者一个或两个表的 FIELD1 列中存在重复值。

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.可以尝试检查两个表的数据,保证join条件正确,FIELD1列没有重复值,对两个表的FIELD1列添加唯一约束,使用子查询限制在连接语句中被删除的行。

Use the ROW NUMBER () function to rank the rows and delete only the top rank.使用 ROW NUMBER() function 对行进行排名,只删除排名靠前的行。

You can also try to use a 'WHERE EXISTS' clause.您也可以尝试使用“WHERE EXISTS”子句。

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. IBM i(又名 DB2-400)的 Db2 不允许使用连接的定位删除,即来自 cursor。

AMarc's suggestion might work, once you fix the syntax...I believe this is correct.一旦您修复了语法,AMarc 的建议可能会奏效……我相信这是正确的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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