Try either multi-table update syntax

UPDATE config t1 JOIN config t2
    ON t1.config_name = 'name1' AND t2.config_name = 'name2'
   SET t1.config_value = 'value',
       t2.config_value = 'value2';

Here is a SQLFiddle demo

or conditional update

UPDATE config
   SET config_value = CASE config_name 
                      WHEN 'name1' THEN 'value' 
                      WHEN 'name2' THEN 'value2' 
                      ELSE config_value
                      END
 WHERE config_name IN('name1', 'name2');

Here is a SQLFiddle demo

Answer from peterm on Stack Overflow
🌐
Medium
medium.com › geekculture › update-multiple-rows-in-sql-with-different-values-at-once-7d2eddb0b85f
Update multiple rows in SQL with different values at once | by Tadej Golobic | Geek Culture | Medium
June 12, 2021 - So, we will join this data from this select statements and set our firstName as new value and id as our condition like this · UPDATE users u JOIN ( SELECT 1 as id, 'myFirstName1' as firstName UNION ALL SELECT 2 as id, 'myFirstName2' as firstName UNION ALL SELECT 3 as id, 'myFirstName3' as firstName ) a ON u.id = a.id SET u.firstName = a.firstName;
Discussions

sql server - SQL update multiple rows in destination table with same id but different values from source table - Database Administrators Stack Exchange
You stated that the order of the ... to ORDER BY in table 1 to create a guaranteed order to match the other table and there is no way in SQL Server to order the rows after insertion date, because information about that is not stored. With this in mind it’s not possible to do a matching with the result you want. There is a solution to update the rows with an arbitrary match within each id... More on dba.stackexchange.com
🌐 dba.stackexchange.com
August 10, 2017
sql server - Update multiple Ids when SET = (Sql Query) - Database Administrators Stack Exchange
I want to update multiple records however the value being set is based on the primary key of the given record. This is not an issue with a single record as I can use: UPDATE T1 SET A1 = (SELE... More on dba.stackexchange.com
🌐 dba.stackexchange.com
sql server - How to update multiple columns of multiple rows in one SQL statement - Database Administrators Stack Exchange
Again, see the SQLFiddle! You can also use a JOIN to update the target record(s). I would encourage you to experiment with these techniques - very useful! Your first result for two (i.e. inserted values) will look like this: record_id two_first_var two_second_var two_third_var 1 21 21 21 2 22 22 22 3 23 23 23 ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. 4 Select multiple rows ... More on dba.stackexchange.com
🌐 dba.stackexchange.com
How To update multiple rows at the same time in SQL
Probably not the best idea unless they are both going up by the same amount or the same percentage. There has to be something in common. More on reddit.com
🌐 r/SQL
22
7
June 13, 2021
Top answer
1 of 2
8

Couldn't find a SQL Server 2008 fiddle engine so I had to opt for a SQL Server 2014 ... so not sure if the following will work in SQL Server 2008, but fwiw ...

Setup some sample data:

create table Table1(id int, Date datetime null);
create table Table2(id int, Date datetime);

insert Table1 values (1,null)
insert Table1 values (1,null)
insert Table1 values (2,null)
insert Table1 values (2,null)
insert Table1 values (2,null);

insert Table2 values (1,'2013-01-29 08:50:00.000')
insert Table2 values (1,'2013-01-29 15:28:00.000')
insert Table2 values (2,'2013-01-31 11:56:00.000')
insert Table2 values (2,'2013-03-11 16:08:00.000')
insert Table2 values (2,'2013-01-31 14:04:00.000');

Keeping in mind that we haven't been provided (yet) with any means to determine which rows to match between Table1 and Table2 for a given id value, I'll just let row_number() generate a 'matching' rowid.

And then we'll make use of SQL Server's ability to update Table1 via a derived table definition:

update T1 
set    T1.Date=T2.Date

from   (select row_number() over(partition by id order by Date) as rowid,
               id,
               Date
        from   Table1 
        where  Date is NULL) T1

join   (select row_number() over(partition by id order by Date) as rowid,
               id,
               Date
        from   Table2) T2

on      T1.id    = T2.id
and     T1.rowid = T2.rowid;

And the results:

select * from Table1;

id  Date
--- --------------------
1   2013-01-29T08:50:00Z
1   2013-01-29T15:28:00Z
2   2013-01-31T11:56:00Z
2   2013-01-31T14:04:00Z
2   2013-03-11T16:08:00Z

And here's a SQL Fiddle for the above.

2 of 2
3

You stated that the order of the matching matters but it seems like you don't have anything to ORDER BY in table 1 to create a guaranteed order to match the other table and there is no way in SQL Server to order the rows after insertion date, because information about that is not stored. With this in mind it’s not possible to do a matching with the result you want. There is a solution to update the rows with an arbitrary match within each id. If that would be good enough.

UPDATE t 
SET    t.[date] = tt.[date] 
FROM   (SELECT *, 
               Row_number() 
                 OVER ( 
                   partition BY id 
                   ORDER BY [date]) AS rno 
        FROM   Table1) AS t 
       INNER JOIN (SELECT *, 
                          Row_number() 
                            OVER ( 
                              partition BY id 
                              ORDER BY [date]) AS rno 
                   FROM   Table2) AS tt 
               ON t.id = tt.id 
                  AND t.rno = tt.rno 

This solution will match all rows individually but can't guarantee the order.

DB Fiddle

🌐
TablePlus
tableplus.com › blog › 2018 › 11 › how-to-update-multiple-rows-at-once-in-mysql.html
How to update multiple rows at once in MySQL? | TablePlus
November 12, 2018 - UPDATE students s JOIN ( SELECT 1 as id, 5 as new_score1, 8 as new_score2 UNION ALL SELECT 2, 10, 8 UNION ALL SELECT 3, 8, 3 UNION ALL SELECT 4, 10, 7 ) vals ON s.id = vals.id SET score1 = new_score1, score2 = new_score2;
Find elsewhere
Top answer
1 of 2
5

You could supply the new values as a table (with the help of the VALUES row constructor), so that you could join it with the target table and use the join in the UPDATE statement, like this:

UPDATE
  tgt
SET
  Column1 = src.Column1,
  Column2 = src.Column2,
  Column3 = src.Column3,
  ...
FROM
  dbo.TargetTable AS tgt
  INNER JOIN
  (
    VALUES
    (1, 'a', 'k', 'x', ...),
    (2, 'b', 'l', 'y', ...),
    (3, 'c', 'm', 'z', ...)
  ) AS src (ID, Column1, Column2, Column3, ...)
    ON tgt.ID = src.ID
;
2 of 2
5

An example of how this can be done (see SQLFiddle here):

(p.s. I used a CTE (aka the WITH clause) and PostgreSQL (I don't use MS SQL Server) but the principles are very much the same - except for the SERIAL datatype - use MS's auto-incrementing type!).

Create and populate a source table (named one):

CREATE TABLE one
(
  record_id SERIAL,
  one_first_var INTEGER,
  one_second_var INTEGER,
  one_third_var INTEGER
);

INSERT INTO one (one_first_var, one_second_var, one_third_var) VALUES (1, 1, 1);
INSERT INTO one (one_first_var, one_second_var, one_third_var) VALUES (2, 2, 2);
INSERT INTO one (one_first_var, one_second_var, one_third_var) VALUES (3, 3, 3);

And also a target table (two):

CREATE TABLE two
(
  record_id SERIAL,
  two_first_var INTEGER,
  two_second_var INTEGER,
  two_third_var INTEGER
);

INSERT INTO two (two_first_var, two_second_var, two_third_var) VALUES (21, 21, 21);
INSERT INTO two (two_first_var, two_second_var, two_third_var) VALUES (22, 22, 22);
INSERT INTO two (two_first_var, two_second_var, two_third_var) VALUES (23, 23, 23);

(double check your values in table two):

SELECT * FROM two;

And then run your update (multiple columns at a time):

WITH my_values AS
(
  SELECT 
         one_first_var, 
         one_second_var, 
         one_third_var
  FROM one
  WHERE one_first_var = 2
)
UPDATE two 
SET 
  two_first_var = my_values.one_first_var,
  two_second_var = my_values.one_second_var,
  two_third_var = my_values.one_third_var
FROM
  my_values
WHERE
  two_second_var = 22;

And then re-run your

SELECT * FROM two;

Again, see the SQLFiddle!

You can also use a JOIN to update the target record(s). I would encourage you to experiment with these techniques - very useful!

Your first result for two (i.e. inserted values) will look like this:

record_id   two_first_var   two_second_var  two_third_var
        1              21               21             21
        2              22               22             22
        3              23               23             23

and your second (updated) result will be:

record_id   two_first_var   two_second_var  two_third_var
        2               2                2              2
        1              21               21             21
        3              23               23             23
🌐
DotFactory
dofactory.com › sql › update
SQL UPDATE
UPDATE Supplier SET City = 'Oslo', ... INSERT - adds a single or multiple records in the table ... One way to update multiple rows is to write multiple UPDATE statements....
🌐
DEV Community
dev.to › chidioguejiofor › scenario-1-making-updates-to-multiple-fields-56hl
Scenario 1: When You Need to Update Multiple Rows with Different Values – The Smart Way! - DEV Community
September 30, 2024 - Here's an example of the generated SQL: UPDATE tickets SET amount = CASE WHEN id = :ticket_id_0 THEN :ticket_value_0 WHEN id = :ticket_id_1 THEN :ticket_value_1 WHEN id = :ticket_id_2 THEN :ticket_value_2 WHEN id = :ticket_id_3 THEN :ticket_value_3 ELSE amount END WHERE id IN (:ticket_ids); The :ticket_id_n and :ticket_value_n placeholders will be replaced by the corresponding values in the replacements object provided to Sequelize.
🌐
Quora
quora.com › How-do-I-update-multiple-rows-of-a-single-column-in-SQL
How to update multiple rows of a single column in SQL - Quora
Answer: Actually that’s real easy. The problem is trying NOT to update the rows you don’t want updated. Let’s assume that you have a column where a few, many or all of the values have a NULL value, but, for whatever reason they should have a value like true or false.
🌐
W3Schools
w3schools.com › sql › sql_update.asp
SQL UPDATE Statement
SQL Examples SQL Editor SQL Quiz SQL Exercises SQL Server SQL Syllabus SQL Study Plan SQL Bootcamp SQL Certificate SQL Training ... The UPDATE statement is used to update or modify one or more records in a table.
🌐
Quora
quora.com › In-SQL-is-it-possible-to-update-entries-in-multiple-rows-of-the-same-column-in-a-single-statement
In SQL, is it possible to update entries in multiple rows of the same column in a single statement? - Quora
Answer (1 of 8): > In SQL, is it possible to update entries in multiple rows of the same column, in a single statement? UPDATE: Thanks to User-9594919546600660871 and Swastik Bhat for reminding me about the [code ]CASE[/code] construct. It’s defined in SQL-92, so every SQL DB worthy of the ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 1391320 › multiple-rows-updation-in-sql-server
Multiple rows updation in sql server - Microsoft Q&A
October 14, 2023 - CREATE TABLE [dbo].[Products]( [Id] [INT] IDENTITY(1,1) NOT NULL, [ProductName] [NVARCHAR](MAX) NULL, [CurrentQuantity] [DECIMAL](10, 2) NULL, CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] EF Core code generated by EF Power Tools.
Top answer
1 of 1
1

If you don't care which timestamp is used for which target row, you can match the two tables by joining them on a generated "row number" using e.g. this:

with cn as (
  select id, invoice_id, generated_at, row_number() over (order by id) as rn
  from creditnote
  where status = 'generated'
), pnr as (
  select id, invoice_id, generated_at, row_number() over (order by id) as rn
  from paymentmodeamount
  where payment_mode = 'creditNote'
)
select *
from pnr 
  join cn on cn.rn = pnr.rn and cn.invoice_id = pnr.invoice_id;

This joins the rows based on the ordering of the ID values in the tables creditnote and paymentmodeamount. If you want you can also order the creditnote table by generated_at.

Given your sample data this would return something like this (this is only to demonstrate what the above join does):

id    | invoice_id | generated_at | rn | id    | invoice_id | generated_at        | rn
------+------------+--------------+----+-------+------------+---------------------+---
63725 |        111 |              |  1 | 10101 |        111 | 2018-02-28 14:42:39 |  1
98236 |        111 |              |  2 | 23982 |        111 | 2018-03-30 11:11:11 |  2

Now the above query can be used as part of an UPDATE statement to bring the rows together.

with cn as (
  select id, invoice_id, generated_at, row_number() over (order by id) as rn
  from creditnote
  where status = 'generated'
), pnr as (
  select id, invoice_id, generated_at, row_number() over (order by id) as rn
  from paymentmodeamount
  where payment_mode = 'creditNote'
)
update paymentmodeamount p
   set generated_at = cn.generated_at
from pnr 
  join cn on cn.rn = pnr.rn and cn.invoice_id = pnr.invoice_id
where p.id = pnr.id
  and p.generated_at is null;

This assumes that paymentmodeamount.id is the primary key of the table.

Note that this won't be very fast for large tables.

Online example: https://rextester.com/YXAK25669

🌐
DEV Community
dev.to › azophy › how-to-update-multiple-rows-based-on-list-of-key-val-pairs-in-mysql-mariadb-postgresql-4lpp
How to update multiple rows based on list of key-val pairs (in MySQL, MariaDB, & PostgreSQL) - DEV Community
September 29, 2023 - MariaDB's VALUES clause is shorter as it doesn't use the ROW keyword at all · UPDATE table_name SET changed_col = temp_data.column1 FROM (VALUES ('key1', 'val1'), ('key2', 'val2'), .... ) as temp_data WHERE comparison_col = temp_data.column0 · Postgres definitely have the best syntax among the three, as it support aliasing column names for VALUES syntax, just as SQL Server did
🌐
Quora
quora.com › How-do-you-update-multiple-values-in-SQL
How to update multiple values in SQL - Quora
Syntax: UPDATE table SET col1 = ... || title, updated_at = CURRENT_TIMESTAMP WHERE department_id = 10; ... Use a WHERE clause that targets multiple rows....