How can I add a computed column to a table that already exists? S.O. has Computed Column Help - TSQL but no information about adding them.
Asked
Active
Viewed 1.1e+01k times
1 Answers
120
The syntax I was looking for is:
alter table TABLE_NAME
add [column_name] as (**COLUMN-SQL**)

Chris Pfohl
- 18,220
- 9
- 68
- 111
-
2`alter table TABLE_NAME add [column_name] as (**COLUMN-SQL**)` – David Knight Oct 31 '13 at 16:31
-
6EX: `alter table TABLE_NAME add [double_count] as (count * 2)` – Veeresh Honnaraddi Dec 02 '15 at 05:10
-
27To create a persisted column (calculated when the data is inserted) you can add the Persisted keyword: alter table TABLE_NAME add [column_name] as (COLUMN-SQL) **PERSISTED** – Sal Sep 23 '16 at 19:47
-
THis does now work when adding Row_Number column. ALTER TABLE MYDB.[dbo].[Table1] ADD Rn INT NOT NULL AS (ROW_NUMBER() OVER (PARTITION BY [MyColumn] ORDER BY [MyColumn])); – Data Engineer Mar 05 '19 at 17:30
-
2I imagine that's a result of using a windowing function in a calculated column. – Chris Pfohl Mar 05 '19 at 17:33