Search This Blog

Monday, January 20, 2014

Add computed column SQL Server

SQL Server > DDL > Alter > Add computed column




You can add computed column with AS keyword followed by your computed formula.
Note: the computed column cannot be updated!

CREATE TABLE #tmp
(
     [hours] float,
     [rate]  float
)
ALTER TABLE #tmp ADD salary AS ([hours] * [rate])
INSERT INTO #tmp([hours], [rate])
SELECT 160, 10
UNION
SELECT 180, 12
SELECT * FROM #tmp
--The column "salary" cannot be modified because it is either a computed column or is the result of a UNION operator.
--update  #tmp
--set salary = salary *1.14
drop table #tmp