NVARCHAR The size (8000) given to the parameter exceeds the maximum allowed (4000)

When building dynamic TSQL query string with nvarchar type in MS SQL Server, it is possible to exceed the limit  of 4000 characters, this will causes query string to be truncated and fail to run.

I have no luck with ntext type : The text, ntext, and image data types are invalid for local variables.

varchar is the correct type to use — it has a  limit of 8000 characters

you can define your query string as below:

declare @@querystring varchar(max)


declare @@querystring varchar(8000)