If you’re getting an error that reads something like “Cannot alter column ‘c2’ because it is ‘COMPUTED’” in SQL Server, it looks like you’re trying to alter a computed column.
We can’t alter computed columns.
To fix this issue, drop the computed column and recreate it with its new definition.
Example of Error
Suppose we create the following table:
CREATE TABLE dbo.t1
(
c1 int,
c2 AS SYSDATETIME()
);
This table uses the SYSDATETIME()
function, which makes it a computed column.
And now let’s try to alter the computed column to use a different definition:
ALTER TABLE dbo.t1
ALTER COLUMN c2 datetime2(7);
Output:
Msg 4928, Level 16, State 1, Line 1
Cannot alter column 'c2' because it is 'COMPUTED'.
As expected, we got an error.
Solution
If we want to change the computed column’s definition, we’ll need to drop it and create it again:
ALTER TABLE dbo.t1
DROP COLUMN c2;
ALTER TABLE dbo.t1
ADD c2 datetime2(7);
Output:
Commands completed successfully.
Success! The column was dropped and recreated with the new definition. In this case I created it not as a computed column, but as a regular column, but that’s not to say we couldn’t create it again as a computed column with a different definition.