Alter column fails due to “hidden” statistic
-
03-03-2021 - |
Question
I'm trying to change a column data type from datetime2
to datetimeoffset
. I have db owner permissions.
Whenever I run the query:
alter table dbo.<my-table>
alter column [TimeStamp] datetimeoffset
I get the following error message:
The statistics 'Stat_a41cbd7f968a4cba86199c960f6f7c3a' is dependent on column 'TimeStamp'.
ALTER TABLE ALTER COLUMN TimeStamp failed because one or more objects access this column.
However, it looks like this statistic 'Stat_a41cbd7f968a4cba86199c960f6f7c3a' is not present in my sys.stats
table, which is very odd.
Is there any other location it might be defined?
Edit: There isn't a single entry in sys.stats
named 'Stat_a41cbd7f968a4cba86199c960f6f7c3a'. In addition, I have never defined any custom statistics other than the automatic ones.
Solution
After reaching out to Azure support, I have managed to solve the issue.
Part of the support engineer's answer included the following:
I've reviewed internal telemetry and can confirm that the statistic listed in the error does not exist. This is likely a compute-node-level statistic that wasn't cleaned up properly.
I was able to confirm and fix this issue by doing the following steps:
- Run a CTAS to create a copy of the table
- Rename the previous table to something different (eg: ) and the new one to the original table name
- DROP the old table
After that, the ALTER TABLE
statement worked as expected.