How to execute an UPDATE only if one row would be affected?

后端 未结 5 944
隐瞒了意图╮
隐瞒了意图╮ 2021-01-21 12:44

I have a table in SQL Server that has a PK (ID) and another (logical) primary key made by a couple of other columns (although there is no UNIQUE constraint on that)

5条回答
  •  迷失自我
    2021-01-21 13:12

    something like that should do it.

    UPDATE Person p1
    SET p1.AGE = 43
    WHERE NAME = 'XX' AND SURNAME = 'YY'
    AND NOT EXISTS (
      SELECT NULL FROM Person p2
      WHERE p1.NAME = p2.NAME
      AND p1.SURNAME = p2.SURNAME
      GROUP BY p2.NAME, p2.SURNAME
      HAVING COUNT(*) > 1)
    

提交回复
热议问题