Validate on write: before setting ParentId = @newParent for node @id, run a recursive CTE from @newParent up to the root and reject the move if @id appears among its ancestors. Do the check and update in one transaction with UPDLOCK, HOLDLOCK (or serializable) on the affected rows so two concurrent moves can't create a cycle together. Add CHECK (ParentId <> Id) for the trivial case.
Checks whether they fix bad data at write time, not only at read time.
Last updated 23 Nov 2026.