E
E
eliasum2020-04-05 17:07:22
Transact SQL
eliasum, 2020-04-05 17:07:22

How to update multiple rows with one MS SQL query?

Hello! There is a table with two rows:
5e89e5addc26c220217639.jpeg
I need to update either one row or both at the same time, and in each case the Lat and Lng fields are updated together. Wrote a request:

INSERT INTO MyGMap (id, Lat, Lng) VALUES (1,'1','2'), (2,'3','4') ON DUPLICATE KEY UPDATE id = VALUES(id), Lat=VALUES(Lat), Lng=VALUES(Lng)

Gives an error that "invalid syntax near ON", and also underlines VALUES, id, Lat and Lng. Tell me what's wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Tsvetkov, 2020-04-05
@eliasum

UPDATE Markers
SET Lat = CASE ID WHEN 1 THEN '1' WHEN 2 THEN '3' END,
    Lng = CASE ID WHEN 1 THEN '2' WHEN 2 THEN '4' END
WHERE ID IN (1, 2)

A
Alexander, 2020-04-05
@Seasle

Update?

UPDATE MyGMap
SET Lat = '1', Lng = '2'
WHERE Id IN (1, 2);

Or add?
INSERT INTO MyGMap (Lat, Lng)
VALUES ('1', '2'), ('3', '4');

UPD:
UPDATE MyGMap as row_1 INNER JOIN MyGMap as row_2
ON
  row_1.id = 1 AND
  row_2.id = 2
SET
  row_1.Lat = '1',
  row_1.Lng = '2',
  row_2.Lat = '3',
  row_3.Lng = '4';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question