2014-04-20 98 views
2

我試圖使用值1更新列,其中以下條件匹配。更正用於在DB2中標記重複行的更新查詢

UPDATE 
(SELECT TBL.KEY, TBL.IS_DUPLICATE FROM MYTABLE AS TBL JOIN 
(SELECT TBL1.KEY FROM MYTABLE AS TBL1 GROUP BY TBL1.KEY HAVING COUNT(TBL1.KEY)>1) SELECTION 
ON SELECTION.KEY = TBL.KEY ORDER BY TBL.KEY ASC) OuterSelection 
SET OuterSelection.IS_DUPLICATE = 1; 

這是我在運行此查詢時遇到的錯誤。

SQL錯誤[42807]:INSERT,DELETE,UPDATE,MERGE或TRUNCATE語句中的目標fullselect,視圖,類型表,物化查詢表,範圍集羣表或臨時表是其目標請求的操作是不允許的。SQLCODE = -150,SQLSTATE = 42807,DRIVER = 4.16.53

+0

http://pic.dhe.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.messages.sql.doc/doc/msql00150n.html 這可能有助於 –

+0

@ChristianCederquist如果我不使用GROUP BY,那麼HAVING如何僅在重複行上進行選擇? – Superman

+0

我可能錯了什麼問題,因爲它提到它不適用於子查詢 –

回答

1

問題是選擇必須在WHERE子句中匹配,並且UPDATE應該只指定表。這裏是解決方案:

UPDATE TBL 
SET IS_DUPLICATE=1 
WHERE KEY IN (SELECT KEY 
FROM TBL 
GROUP BY KEY 
HAVING COUNT(*) > 1);