Using Computed Columns in SQL Server

Hi friends, in this article, we will be discussing about computed columns in SQL Server and see some relevant T-SQL examples.

 

What are Computed and Persisted Columns in SQL Server?

When it comes to database design in SQL Server, it is always recommended to create the database as flexible as possible. SQL Server provides several features for developers in order to be able to do so.

An example of such a feature are the “computed” and “persisted” columns.

As explained on MS Docs, computed columns’ values are computed by expressions that can use other columns in the same table.

Also, when using the PERSISTED keyword with a COMPUTED column, the data of the computed column is physically stored in the table.

Let’s see some code and try out this great feature!

 

An Example of Using Computed and Persisted Columns in SQL Server

First let’s create two tables representing product information.

 

Creating the Tables

Note the column with name “totalPrice”: It is a computed column which computes the total price (price + VAT).

 

CREATE TABLE Product1
(
id int, code varchar(50),
price float,
vatPerc float,
totalPrice as (price+(price*(vatPerc/100)))
)
GO

 

Note again the column with name “totalPrice”: It is the same computed column as in table Product 1 with the difference that it uses the PERSISTED keyword.

CREATE TABLE Product2
(
id int,
code varchar(50),
price float,
vatPerc float,
totalPrice as (price+(price*(vatPerc/100))) PERSISTED
)
GO

 

Testing the Tables

Testing Table Product1:

INSERT INTO Product1 (id,code,price,vatPerc)
SELECT 1, 'PRODUCT01',100,15
UNION ALL
SELECT 2, 'PRODUCT02',200,15
GO

* I know, I could use a Table Value Constructor (Row Constructors) but I would prefer that my examples to be compatible with all versions of SQL Server as Table Value Constructors is one of the new programmability features in SQL Server 2008 🙂

Note that in the above INSERT statement I am not inserting any values for the “totalPrice” column. Though, when taking a look at the generated results, the totalPrice values are calculated on the fly!

SELECT *
FROM Product1
GO

 

Output:

Using Computed Columns in SQL Server - Article on SQLNetHub

 

Similarly, testing table Product2 will work the same way with the only difference that this time, the calculated values for the “totalPrice” column will be physically stored in the table:

Testing Table Product2:

INSERT INTO Product2 (id,code,price,vatPerc)
SELECT 1, 'PRODUCT03',300,15
UNION ALL
SELECT 2, 'PRODUCT04',400,15
GO

SELECT *
FROM Product2
GO

 

Output:

Using Computed Columns in SQL Server - Article on SQLNetHub

 

* Note: By the time you are using a Computed column you cannot explicitly insert data into it. The data is computed based on the computed column’s definition (expression).

 

Learn more about SQL Server Development – Enroll to our Course!

Enroll to our online course titled “Essential SQL Server Development Tips for SQL Developers(special limited-time discount included in link) and sharpen your SQL Server database programming skills via a large set of tips on T-SQL and database development techniques. The course, among other, features over than 30 live demonstrations!

Essential SQL Server Development Tips for SQL Developers - Online Course
(Lifetime Access/ Live Demos / Downloadable Resources and more!)

Learn More

 

 

Featured Online Courses:

 

Read Also:

 

Subscribe to our newsletter and stay up to date!

Subscribe to our YouTube channel (SQLNetHub TV)

Check our latest software releases!

Check our eBooks!

 

Rate this article: 1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

Loading...

Reference: SQLNetHub.com (https://www.sqlnethub.com)

© SQLNetHub