[SqlServer] How can I modify data in another table with data in one table?

Source: Internet
Author: User

Q: How can I modify the data in another table using the data in one table based on certain conditions?

A: How difficult is this? use SQL UPDATE.

Table 1

Student

Stu_id Stu_name Stu_age
1 Aa 20
2 Bb 21
3 Cc 22
4 Dd 23

The other table is as follows:

Table 2

Lag

Lag_id Lag_name Lag_another
1 1111 Abcd
3 2222 Efgh

I want to replace the data in student 1 and 3 with 1111 and 2222 in tables 1 and 3 in lag. The modified results are as follows:

Table 1

Student

Stu_id Stu_name Stu_age
1 1111 20
2 Bb 21
3 2222 22
4 Dd 23

The execution statement is as follows:

Update student
Set student. stu_name = (Select lag. lag_name from lag where lag. lag_id = student. stu_id)
Where student. stu_id in (Select lag. lag_id from lag)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.