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)