Maybe I missed a step or something, but I just tested your script against a test database using SQL Agent and it ran without errors.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning