For the most part, if you have defined a linked server, there is usually no need at all for OPENQUERY or anything like it. Just use a 4 part naming convention when referring to the tables in the linked server.
The other thing is that you used the word "loop" a lot... generally speaking that's a form of death by SQL and should be avoided.
Without seeing the code, there's not much else I can offer.... but if you've made a query so big that SQL Server hit the character limit on it, then you've made a query with hundreds of thousand of characters... don't post it here, please. I suspect it's something else wrong, though.
Last, but not least, it would be handy if you posted the actual error messages. We're just guessing here.
--Jeff Moden
Change is inevitable... Change for the better is not.