| |

VerySource

 Forgot password?
 Register
Search
View: 932|Reply: 6

How to write this sentence? Thank you

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 United States

Post time: 2020-3-3 05:30:01
| Show all posts |Read mode
sqlserver has two tables

Form A
code name status
1 a 01
2 b 02
3 c 03

Form B
code status
1 01
2 01
3 01

Now I want to modify the status of table A according to the status of table B, all are changed to 01, the associated field is code, how to write such a statement?
Reply

Use magic Report

0

Threads

12

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-5-13 21:30:02
| Show all posts
update A
set
    status = B.status
from
    B
where
    A.code = B.code
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 Egypt

Post time: 2020-5-13 22:15:01
| Show all posts
update a set a.status = b.status
from a, b
where a.code = b.code
Reply

Use magic Report

0

Threads

40

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-5-13 23:00:02
| Show all posts
update A set A.status = B.status where A.code = B.code
Reply

Use magic Report

0

Threads

114

Posts

69.00

Credits

Newbie

Rank: 1

Credits
69.00

 China

Post time: 2020-5-14 06:30:01
| Show all posts
update tablea set status = b.status from tableb b where code = b.code
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 China

Post time: 2020-5-14 09:00:01
| Show all posts
create table A (code int, name char (1), status char (2))
insert A select 1, 'a', '01'
union all select 2, 'b', '02'
union all select 3, 'c', '03'

create table B (code int, status char (2))
insert B select 1, '01'
union all select 2, '01'
union all select 3, '01'

update A set A.status = B.status
from B
where A.code = B.code

select * from A

--result
code name status
----------- ---- ------
1 a 01
2 b 01
3 c 01

(3 row (s) affected)
Reply

Use magic Report

0

Threads

40

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-5-15 09:45:01
| Show all posts
update A
set A.status = B.status
from A, B
where A.code = B.code
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list