I have done some debugging on my sql and I cant figure out the maddening error I am getting I have narrowed it down to a couple of lines which I cant see what the problem is
You're missing an opening square bracket here:
... ') SELECT ' + @cols2 + ']' + ' FROM ...
Furthermore I would recommend you switch to using the QuoteName() function instead:
... ') SELECT ' + QuoteName(@cols2) + ' FROM...