How do you create a non-clustered index for a string column in SQL Server? [duplicate]

StackOverflow https://stackoverflow.com/questions/15262083

  •  18-03-2022
  •  | 
  •  

Domanda

I just tried this and got the helpful message that

Column 'Username' in table 'Users' is of a type that is invalid for use as a key column in an index.

Username is an nvarchar(max) column.

È stato utile?

Soluzione

You can't add a index to a nvarchar(max) or varchar(max) column but it can be part of an index.

You can include it in an index but then that index needs to created/rebuilt offline.

Taking a guess from your column name 'Username' I would change the column type to something smaller like a nvarchar(50). The nvarchar(max) datatype can store up to 4Gb of text and is used for more full text storage (in that use you would setup full text searching).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top