Lowell (7/10/2013)
instead ofEXEC sp_executesql @sql
use
EXEC(@sql)
which has no limits on the query size, since it's not parameterized. ou are not passing parameters via sp+executesql, so you'd be good to go, i think.
Extending this suggestion - you can also execute a string at the remote end with EXECUTE ... AT:
EXEC('TRUNCATE TABLE mydb.dbo.' + @tablename) AT LinkedServerName
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