如果行重复,如何更新一行(只有一行)? DB2

时间:2015-03-24 16:08:35

标签: database db2

我可以在选择时添加一个rownumber,但我无法弄清楚如何在更新命令中使用行号

    SELECT ROWNUMBER() OVER (ORDER BY numb) AS CODE, numb FROM myDB2.tblRep
    WHERE comp = 'FV'
        AND TRIM(UPPER(numb)) LIKE UPPER(TRIM('01key%'))

我也尝试了类似于这里的内容(http://bytes.com/topic/db2/answers/446226-update-duplicate-rows),在我的代码中是这样的:

    SELECT * FROM myDB2.tblRep AS o
    WHERE o.comp = 'FV'
      AND TRIM(UPPER(o.numb)) LIKE UPPER(TRIM('01key%'))
      AND ( SELECT COUNT(*)
    FROM myDB2.tblRep  AS i
    WHERE o.numb = i.numb ) > 1

我总是在进行更新之前进行选择,但它会返回带有重复的行,所以我不相信这会有效

想法?请

2 个答案:

答案 0 :(得分:0)

这可能只是

UPDATE (
  SELECT * FROM myDB2.tblRep
  WHERE comp = 'FV' AND TRIM(UPPER(numb)) LIKE UPPER(TRIM('01key%'))
  FETCH FIRST 1 ROW ONLY
)
SET yourcolumn = 'yourvalue'

这适用于所有最新的DB2版本。

答案 1 :(得分:0)

类似的东西:

UPDATE (
    SELECT ..., ROWNUMBER() OVER (PARTITION BY ...
                                  ORDER BY ...) AS RN 
    FROM myDB2.tblRep
    WHERE comp = 'FV'
      AND TRIM(UPPER(numb)) LIKE UPPER(TRIM('01key%'))
) SET the_col = ...
WHERE RN = 1;