What is Computed Column?
Computed Columns are those columns whose values are not stored in the table. Instead they will be calculated dynamically. They have a formula that creates them that could be a Function, another Column Reference or a Constant Value.
In the figure below, we can see that ProjectName is a Computed Column. The Column Properties sets the column specification to a user defined method named GetProjectName() passing the ProjectId as argument.

Let us see how we can benefit from it.
Example
We have two tables in this example - Employee and Project. The tables are related through the ProjectId column as shown below:

The Problem
We need to get the ProjectName of each Employee without using join statement as the join statement will create alterations in SQL statement.
The Solution
Here we can introduce the computed column ProjectName which in turns calls a user defined function to get the actual project name based on the ProjectId.
The following is the User Defined Function to do that:
CREATE FUNCTION [dbo].[GetProjectName]
(
@Id int
)
RETURNS varchar(50) AS
BEGIN
DECLARE @Result varchar(50)
SET @Result = (SELECT Name FROM Project WHERE Id=@Id)
RETURN @Result
END
Advantages
The following advantages can be identified from the above scenario:
- Less Overhead - While connecting with ORM tools we do not need to call specific methods to invoke the table or filter on the column.
- Speed Optimization on Filtering - As the calculation and filtering done on the server side only the required rows are passed over the wire. Otherwise without a computed column, the whole table will be fetched and processed thereafter filtering - this will increase the network traffic as well.


Join the conversation! Your thoughts help the community grow.