When I created an Index on a varchar(1024) column using SSMS GUI, it failed with the message:
"Warning! The maximum key length is 900 bytes. The index 'XXX' has maximum length of 1024 bytes. For some combination of large values, the insert/update operation will fail."
However, the same operation succeeded when I used SQL (not GUI). I'm not sure why this happened, but does this mean the index will work until the data over 900 bytes really is inserted into the column?
(The column currently doesn't have value over 900 bytes.)
environment:
– SQL Sever 2012
– Windows Server 2008 R2 SP1
Best Answer
All versions
If you create an index on a column that its definition is longer than 900 or 1700 (depending on version and type of index), you'll either get a warning and the index creation will succeed (if the table is empty or the existing data are not that long) or an error and the index creation will fail (if a row or more exceed the limit).
For the warning case, the limits are:
SQL Server 2014 and Older
If you create a clustered index on a
varchar(>900)
column you will receive a warning when creating the index.If you create a non-clustered index on a
varchar(>900)
column you will receive a warning message when creating the index.SQL Server 2016 and Newer
If you create a clustered index on a
varchar(>900)
column you will receive a warning message when creating the index.If you create a non-clustered index on a
varchar(>1700)
column you will receive a warning message when creating the index.Inserting Data
If you don't insert more data than is limited by the index and the version of SQL Server then you will not receive an error message in your application / SSMS / SQL Query / ...
However, inserting more than the limits specified by the version and index type will result in an error message.
Testing
You can try this out over at db<>fiddle. The links for each test are provided below the test examples.
Varchar Non-Clustered Index Limitations on SQL Server 2012
db<>fiddle here
Varchar Clustered Index Limitations on SQL Server 2012
db<>fiddle here
Varchar Non-Clustered Index Limitations on SQL Server 2016
db<>fiddle here
Varchar Clustered Index Limitations on SQL Server 2016
db<>fiddle here
Summary
Your Question
It will work with the above limitations.
References