Skip to content
Advertisement

How to create a Table Type with columns more than 1024 columns

I want to create a table type that should have more than 1024 columns. So I tried to use sparse columns by creating a – SpecialPurposeColumns XML COLUMN_SET as shown below. That did not work. It gave me an error: Incorrect syntax near ‘COLUMN_SET’

CREATE TYPE dbo.TempTable AS TABLE (
        ID INT IDENTITY(1,1),
        SpecialPurposeColumns XML COLUMN_SET FOR ALL_SPARSE_COLUMNS,
        col1 VARCHAR(10) SPARSE NULL,
        col2 VARCHAR(10) SPARSE NULL,
        .
        .
        col1025 VARCHAR(10) SPARSE NULL);

Is there any way that I can create a table type that can have more than 1024 columns?

Advertisement

Answer

From Restrictions for Using Sparse Columns:

Restrictions for Using Sparse Columns

Sparse columns can be of any SQL Server data type and behave like any other column with the following restrictions:

  • A sparse column cannot be part of a user-defined table type, which are used in table variables and table-valued parameters.

So you cannot use SPARSE columns in a table type object.

As for having more than 1,024 columns, again, no you can’t. From Maximum capacity specifications for SQL Server:

Database Engine objects

Maximum sizes and numbers of various objects defined in SQL Server databases or referenced in Transact-SQL statements.

SQL Server Database Engine object Maximum sizes/numbers SQL Server (64-bit) Additional Information
Columns per table 1,024 Tables that include sparse column sets include up to 30,000 columns. See sparse column sets.

Obviously, the “see sparse column sets” is not relevant here, as they are not supported (as outlined above).

If, however, you “need” this many columns then you more than likely really have a design flaw; probably suffer from significant denormalisation.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement