• This is a nice bit of code on smaller chunks of XML.

    It might also be worth adding exec "sp_xml_removedocument @idoc" to the end of the procedure to free up memory.