【问题标题】:Update ignore unique constraint key if the value is already on the table. - ORACLE如果值已经在表上,则更新忽略唯一约束键。 - 甲骨文
【发布时间】:2015-08-14 04:51:45
【问题描述】:

我有这个 oracle 命令。

UPDATE TABLE_NUMBERS
SET mdn = concat(concat(substr(mdn,1,2),`9`,substr(mdn,3,9))
WHERE mdn LIKE `8%` AND LENGTH(mdn)=10;

如果数字以 8 开头并且有 10 位,我必须将数字 9 放在 3 的位置。

假设我有8187412868,它将是81987412868

但是819874128688187412868 已经摆在桌面上了。

在这种情况下,不需要更新或删除,但我想忽略唯一约束错误来执行整个查询。

喜欢

    if (concat(concat(subsrtr(mdn,1,2),`9`,substr(mdn,3,9))
    WHERE mdn LIKE `8%` AND LENGHT(mdn)=10)
    already on the table, then ignore
    else
    execute....

【问题讨论】:

    标签: sql oracle select oracle11g oracle10g


    【解决方案1】:

    您可以使用exists 运算符来预先检查此值是否存在(另外,请注意我修复了 OP 查询中的括号和拼写错误):

    UPDATE table_numbers a
    SET    mdn = CONCAT(CONCAT(SUBSTR(mdn, 1, 2), '9') ,SUBSTR(mdn, 3, 9))
    WHERE  mdn LIKE '8%' AND 
           LENGTH(mdn) = 10 AND
           NOT EXISTS (SELECT *
                       FROM   table_numbers b
                       WHERE  a.mdn = CONCAT(CONCAT(SUBSTR(b.mdn, 1, 2), 
                                                    '9'), 
                                             SUBSTR(b.mdn, 3, 9))
    

    【讨论】:

    • 感谢编辑。拳头时间在这里。你的 sql 缺少 1 个“)”,但即使这样我也得到了相同的 msg。 “违反了唯一约束”
    猜你喜欢
    • 2017-02-22
    • 2014-03-17
    • 2011-11-26
    • 2012-10-25
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    相关资源
    最近更新 更多