Nice code brah, but gives me an error message:
Msg 319, Level 15, State 1, Procedure fn_GetLockChainSubTree, Line 11
Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near ' '.