Which column of the VALUES clause of the INSERT statement is it?
'V'+@retval + Replace( Str( @max-2 + @count, 7), ' ', '0') ?
seems likely, but you have no column list to insert into.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden