Hello,
Â
Scenario – I am trying to update a column in a given table with different values. These values have to be taken from another table in the same database.
Challenge- I want to update these different values in each row using a single update statement.
Looking for – Please suggest me an efficient way of doing this along with an example.
Updating multiple rows using single update statement
Dear Aayu,
If im not mistaken you want to update your rows from table1 on column1 with value from table2 .If thats correct then you need to join the tables .Â
You can see the example below :
UPDATE table1 SET column1 = table2.column1 FROM table1, table2 WHERE table1.pk = table2.pk
note : pk >> primary key or any other criteria you want
Hope thats answer your question
Â
Updating multiple rows using single update statement
Hello Aayushjain02,
Follow the Syntax of Update and refer to table twice. You can also do it by DATEDIFF function or writing SQL to update columns of a table from columns from another table.
Example is given below:
The data of 2 tables is as follows:
I.   TableA
a   b   c   d
1   x   y   z
2   a   b   c
3   t   x   z
II.   TableB
a1Â Â Â b1Â Â Â c1Â Â Â d1Â Â Â e1
1Â Â Â x1Â Â Â y1Â Â Â z1Â Â Â 40
2Â Â Â a1Â Â Â b1Â Â Â c1Â Â Â 50
Â
Results after the update:
a   b   c   d
————————————
1    x         y          z
2 Â Â Â a1Â Â Â Â Â Â Â b1Â Â Â Â Â Â Â Â c1
3    t          x          z
For more information, please go to the site.
I think, this will help you.
Thanks.