Pages

How to add the computed column to a table based on the columns already exist in the table?

The application contains a table that has the following definition:

CREATE TABLE Items
(ItemID int identity(1,1) NOT NULL PRIMARY KEY,
ItemsInStore int NOT NULL,
ItemsInWarehouse int NOT NULL)

You need to create a computed column that returns the sum total of the ItemsInStore and ItemsInWarehouse values for each row.

ItemID ItemsInStore ItemsInWarehouse
1 30 35
2 400 50
3 11 3

Alter table Items
Add TotalItems AS ItemsInStore + ItemsInWarehouse


ItemID ItemsInStore ItemsInWarehouse TotalItems
1 30 35 65
2 400 50 450
3 11 3 14