skip to Main Content

I have a large number of similar but unique update statements, where the same fields are being updated with different values, ie.

UPDATE tbl SET field1 = "x" WHERE pk = 1;
UPDATE tbl SET field1 = "y" WHERE pk = 2;

These are being sent as parameterised queries using DbCommand. Is there anything I can do to combine these, or otherwise optimise the update process?

2

Answers


  1. Chosen as BEST ANSWER

    I can confirm that @MarcGravell's comment above does work, and allows me to concatenate queries even when the updated fields are different in each query.


  2. You could use a joined update with a VALUES clause

    UPDATE tbl
    JOIN (VALUES
        ROW(@pk1, @v1),
        ROW(@pk2, @v2)
    ) AS v(pk, field1)
    SET tbl.field1 = v.field1;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search