SQL Server updatefrom statement study _ MySQL

Source: Internet
Author: User
This article mainly introduces the SQL Server updatefrom statement. For more information, see update a table and use the where statement:

The code is as follows:


UPDATE Ttest SET
StatusInd = 'active'
WHERE
Id = 123

Note:

The table name after the update statement. aliases cannot be enabled.
At this time, the id field is from the Ttest table (understandable)

However, if there are additional join table condition constraints for update, the statement is as follows:

The code is as follows:


UPDATE Ttest SET
StatusInd = 'active'
FROM
Tparent parent
WHERE
Ttest.id = 123
AND Ttest. parentId = parent. id
AND parent. statusInd = 'active'

The code is as follows:


UPDATE Ttest SET
StatusInd = 'active'
FROM
Ttest parent,
Ttest
WHERE
Ttest.id = 123
AND Ttest. parentId = parent. id
AND parent. statusInd = 'active'

Explanation:

The requirement is: modify its own statusInd attribute to determine whether the statusInd attribute associated with its parent is also 'active'
The table (Ttest) after update cannot be named as an alias!
The table after from is also a Ttest, but the Ttest record to be updated is not the same as the from Ttest record (to update the child, but to associate the child with its parent)

From must be followed by a Ttest without an alias to specify the record of the table, which is to be updated.
The table from join (Ttest) must have an alias to distinguish it from the update table (Ttest)

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.