In your future queries, please, wrap any script blocks you post in [code\][/code] tags ("\" in [code\] is an escape character to show the actual tag. You should use tags without it). Especially, if they are large and contain exceptions. It becomes easier to select for further troubleshooting and looks clearer overall. Corrected for now.
As for the issue - I tested your script on both v10 P2 and v10a VBR instances and it works flawlessly. So, kindly comment on @veremin
questions. Let's see how we can help you.