I have the same problem in a server with SQL 2000. But the code works fine in SQL 2005. One solution may be to replace the table variable by a temporary table (#InputBuffer instead @InputBuffer).
I had 2 minor problems too, here are (and his solution):
1.
...ON P.sid = L.sid
My server is in case sensitive mode, the alias is L, not l
2.
... OR P.program_name LIKE 'SQL Query Analyzer%'
In the original code appears "Analiser"
Hope be useful