So, I am writing a stored procedure in MS SQL Server 2008. This is a very long query, and I have to write it dynamically, so I create a variable called @Query and create it of type NVARCHAR(MAX) . Now I have been told that in modern versions of SQL Server NVARCHAR(MAX) can contain an absurd amount of data, which exceeds the maximum maximum of 4000 characters. However, @Query still truncates to 4000 characters when I try to print it.
DECLARE @Query NVARCHAR(max); SET @Query = 'SELECT...' -- some of the query gets set here SET @Query = @Query + '...' -- more query gets added on, etc. -- later on... PRINT LEN(@Query) -- Prints out 4273, which is correct as far as I can tell PRINT @Query -- Truncates value to 4000 characters EXEC sp_executesql @Query -- totally crashes due to malformed (truncated) query
Am I doing something wrong, or am I completely wrong about how NVARCHAR(MAX) works?
sql sql-server-2008 dynamic-sql
Andrew Arnold Jan 28 2018-11-28T00: 00Z
source share