Arif YILMAZ
Arif YILMAZ

Reputation: 5866

Nvarchar(max) shrinks to 4000 chars in SQL Server

I am creating a dynamic query in stored procedure, and when I try to print that dynamic query, it only prints a part of the whole query.

My variable that holds the whole dynamic query is declared like below

DECLARE @SQL NVARCHAR(MAX)

When I print the variable's length like below, It gives the length of the whole query, which is good.

PRINT LEN(@SQL)

But, when I print the script itself, It prints only a part of the whole query.

PRINT @SQL

I also tried to print it like below

PRINT CONVERT(NVARCHAR(MAX),@SQL)

Why it only prints first 4000chars? What am I doing wrong?

Upvotes: 2

Views: 5127

Answers (3)

Paul Andrew
Paul Andrew

Reputation: 3253

It's PRINT that is limited to 4000 characters, not the NVARCHAR(MAX) variable.

A workaround would be to just something like this PRINT BIG function with your string if you want to output everything.

https://www.richardswinbank.net/doku.php?id=tsql:print_big

You could also just SELECT the variable which isn't limited and copy the contents from the results.

Upvotes: 2

bmsqldev
bmsqldev

Reputation: 2735

try as below to print entire string,

SET @Query = 'SELECT ....' [Up To 4,000 characters, then rest of statement as below]

SET @Query = @Query + [rest of statement]

Now run your query as normal i.e. EXEC ( @Query )

got the answer from below link

nvarchar(max) still being truncated

Upvotes: 2

Randy Minder
Randy Minder

Reputation: 48392

You are not doing anything wrong. The Print command is limited to outputting 4000 characters (see BOL - Books Online, for more details). It does not mean nvarchar(max) has shrunk to 4000 characters.

Upvotes: 8

Related Questions