Question

What is limited of define Nvarchar(max) columns in SQL Server 2008 R2 ?

I see this link

But, I want get limit number of nvarchar(max) columns in one table.

Was it helpful?

Solution

1024 non-sparse.

30000 sparse.

There is nothing special about the column being NVARCHAR(MAX). You won't be able to create a row with that many non-null values, but that is a different topic.

OTHER TIPS

Is the question how many can be created or how many can be populated? Remus is correct on the number that can be created. Populating them is a different matter.

You can put data in the columns up to 8000 bytes (or so) on the data page. However, if you want to put long data in the columns, then you are left with 24 bytes of overhead on the original page, according to the SQL Server documentation.

Because the page is limited to about 8,000 bytes of data, the practical limit of populated columns is more like 8,000/24 = 333 columns. Of course, this depends on the other columns that are in the table.

I am agree with @RemusRusanu and GordonLinoff.

You can use the following query to create a table with 1024 nvarchar(Max) columns.

CREATE Proc [dbo].[CreateTable_NvarcharMax]
(@TableName nvarchar(100),@NumofCols int)
AS
BEGIN

DECLARE @i INT
DECLARE @MAX INT
DECLARE @SQL VARCHAR(MAX)
DECLARE @j VARCHAR(10)
DECLARE @len int
SELECT @i=1
SELECT @MAX=@NumofCols
SET @SQL='CREATE TABLE ' + @TableName + '('

WHILE @i<=@MAX

BEGIN
select @j= cast(@i as varchar)
SELECT @SQL= @SQL+'X'+@j  +' NVARCHAR(Max) , '
SET @i = @i + 1
END
select @len=len(@SQL)

select  @SQL = substring(@SQL,0,@len-1)


SELECT @SQL= @SQL+ ' )'

exec (@SQL)

END

GO

exec [dbo].[CreateTable_NvarcharMax] 'Test', 1024

This query will be executed successfully and will create table with 1024 columns but give following Message.

Warning: The table "Test" has been created, but its maximum row size exceeds the allowed maximum of 8060 bytes. INSERT or UPDATE to this table will fail if the resulting row exceeds the size limit.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top