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 OverflowTry 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
You can accomplish it with INSERT as below:
INSERT INTO mytable (id, a, b, c)
VALUES (1, 'a1', 'b1', 'c1'),
(2, 'a2', 'b2', 'c2'),
(3, 'a3', 'b3', 'c3'),
(4, 'a4', 'b4', 'c4'),
(5, 'a5', 'b5', 'c5'),
(6, 'a6', 'b6', 'c6')
ON DUPLICATE KEY UPDATE id=VALUES(id),
a=VALUES(a),
b=VALUES(b),
c=VALUES(c);
This insert new values into table, but if primary key is duplicated (already inserted into table) that values you specify would be updated and same record would not be inserted second time.
sql server - SQL update multiple rows in destination table with same id but different values from source table - Database Administrators Stack Exchange
sql server - Update multiple Ids when SET = (Sql Query) - Database Administrators Stack Exchange
sql server - How to update multiple columns of multiple rows in one SQL statement - Database Administrators Stack Exchange
How To update multiple rows at the same time in SQL
Videos
One way to do this is with a case expression:
UPDATE table_name
SET name = CASE id WHEN 7 THEN 'Bob'
WHEN 34 THEN 'Mike'
END
WHERE id IN (7, 34)
This solution is more practival than "case when" I found and you can update as many columns as wanted
UPDATE searches_group_library
SET compiled_query = new_val.compiledq
FROM (VALUES
(28, 'fakecompilation'),
(57, 'fakecompilation2')
) new_val (id, compiledq)
WHERE new_val.id = searches_group_library.id;
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.
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
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
;
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
| Id | Name | Price |
|---|---|---|
| 1 | Pen | 50 |
| 2 | Pencil | 60 |
This is my table structure now I want to update the price of Pen and Pencil at the same time with one SQL query
This for using where clause :
UPDATE employees SET gender = 'Male' WHERE id IN (1,2,3)
If you want update all rows in table then:
UPDATE employees SET gender = 'Male'
You can use the same thing, but for the ID's you can use ID's in
Like this:
....
where id in (1,2,3);
You can also write a nested query inside IN.
For more details on how to write IN you can refer here.