Paras
Paras

Reputation: 3481

Update multiple rows at once in SQL Server

I have a TABLE A with the following schema :

|ID|   |NAME|
-------------
1        A
2        B

I have another table B with the following schema

|ID|   |STATUS|
---------------
1        4
2        5

I need to update the status column to 7 in table B whose ID's corresponds to the ID's in Table A.

Update Table B Set status = 7 where tableB.ID = tableA.id

How can I convert this to equivalent sql query?

Upvotes: 0

Views: 462

Answers (2)

Berka Ayowa
Berka Ayowa

Reputation: 1

Hi after looking at your tables the following code will perform what you want.

UPDATE B SET STATUS = 7 WHERE ID IN (SELECT ID FROM A)

Upvotes: 0

Thom A
Thom A

Reputation: 95558

Personally, I would do:

UPDATE B
SET status = 7
FROM TableB B
     JOIN TableA A ON B.ID = A.ID;

Upvotes: 3

Related Questions