Introduction
Recently during an update of an application I received the following error message:
Msg 5074 Level 16 State 1 Line 1 The statistics 'BreakingStuff' is dependent on column 'TaskText'. Msg 4922 Level 16 State 9 Line 1 ALTER TABLE ALTER COLUMN TaskText failed because one or more objects access this column.
Example error message based on analysis
This was a manually created statistic on a column in a table. Actually there were multiple manually created statistics referencing the column in question.
Workaround
After dropping the statistics for the table, running the update, and then recreating the statistics, everything was running fine.
Analysis
I started out with a trivial simulation on db<>fiddle to see if I could reproduce the issue. After some fiddling (pun intended) I found out that the error message only gets triggered if I reduce the size of the column.
e.g. varchar(50)
--> varchar (40)
Here is the db<>fiddle.
The only warnings I could find on Microsoft Learn were:
-
Modifying the data type of a column that already contains data can result in the permanent loss of data when the existing data is converted to the new type. In addition, code and applications that depend on the modified column may fail. These include queries, views, stored procedures, user-defined functions, and client applications. Note that these failures will cascade. For example, a stored procedure that calls a user-defined function that depends on the modified column may fail. Carefully consider any changes you want to make to a column before making it.
Reference: Modify columns (Microsoft Learn | SQL)
I was aware that reducing the column size would possibly result in loss of data, but this wasn't the case, as the data were all shorter than varchar(40)
.
Questions
- Why does reducing the size of column result in an error, because of the manually created statistics?
- Why does increasing the size of a column not result in an error with the manually created statistics?