Computed columns in SQL Server offer a powerful way to create virtual columns that derive their values from other columns in the same table. These columns are not physically stored in the table (though they can be, if specified), but are calculated dynamically when queried. This feature can help improve data integrity, reduce redundancy, and simplify queries.
Computed Columns can be really useful and powerful when you use them in right place and in right way. Let’s say we have the following inventory table.

We want to have a column in this table which will contain the amount of money we spend for the inventory. (InvoicePrice * InStock) should be good for now. Following command creates a computed column in our table.
ALTER TABLE Products ADD Total AS (InvoicePrice*InStock) PERSISTED
While computed columns are typically not stored in the database, you can choose to persist them. This means the result of the computation will be stored just like a regular column, which can enhance performance but can also require additional storage space. You can use Persisted Computed Columns as FOREIGN KEY, CHECK or NOT NULL constraints.

Leave a Reply