Updating multiple rows using single update statement

Asked By 0 points N/A Posted on -
qa-featured

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.

SHARE
Best Answer by Paul jolie
Answered By 0 points N/A #105580

Updating multiple rows using single update statement

qa-featured

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

 

Best Answer
Best Answer
Answered By 15 points N/A #105581

Updating multiple rows using single update statement

qa-featured

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.

Related Questions