Search code examples
sqlcomposite-keyexcept

SQL UPDATE all rows, except some rows, in table with composite primary key


I'm struggling with a SQL statement.

I want to update all rows except some, in a table with a composite primary key.

This is what I do now:

UPDATE Products SET Active = 0

_

UPDATE Products SET Active = 1
WHERE (Id_A = 1 AND Id_B = 1 AND Id_C = 1) OR
      (Id_A = 1 AND Id_B = 2 AND Id_C = 1) OR
      (Id_A = 5 AND Id_B = 8 AND Id_C = 3) OR
       .
       .
       .
       etc

This works, but I don't like it. I would like to be able to do it one go.

Is there some way to do this in SQL?


Solution

  • You mean something like:

    UPDATE Products SET Active = CASE WHEN
          (Id_A = 1 AND Id_B = 1 AND Id_C = 1) OR
          (Id_A = 1 AND Id_B = 2 AND Id_C = 1) OR
          (Id_A = 5 AND Id_B = 8 AND Id_C = 3) OR
           .
           .
           .
           THEN 1 ELSE 0 END