SQL server update from statement, sqlupdate

Source: Internet
Author: User

SQL server update from statement, sqlupdate

To update a table, use the where statement:

Copy codeThe 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:

Copy codeThe 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'

Note:

Aliases cannot be used for Ttest after update.
Locate the Ttest record to be modified and write it as follows: Ttest. id = 123

If you write id = 123 directly, you cannot tell whether it is the id of the Ttest table or the Tparent table.
This SQL statement indicates whether the statusInd attribute associated with its parent is 'active' if you want to modify its statusInd attribute'

In this case, the hierarchical relationship is defined using two tables (Ttest and Tparent.

However, if the design of a database table only uses a table (Ttest) to express the hierarchical relationship between data (Ttest. parentId = Ttest. id ),
How can we write data for the purpose? (If you want to modify its own statusInd attribute, you must check whether the statusInd attribute associated with its parent is also 'active ')

The implementation is as follows:

Copy codeThe 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)

Related Article

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.