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.
1 Answer
The syntax I was looking for is:
alter table TABLE_NAME
add [column_name] as (**COLUMN-SQL**)
-
2
-
5
-
24To 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, 2016 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])); Mar 5, 2019 at 17:30
-
1I imagine that's a result of using a windowing function in a calculated column. Mar 5, 2019 at 17:33