Microsoft SQL Server 2005
Database Design Best Practices

How to configure a computed column?

 

Execute the following script in Query Editor to demonstrate the creation of a computed column. InventoryListValue is the computed column. SQL Server 2005 doesn't store its value, instead, it computes the value each time it's read.

USE tempdb
GO

CREATE TABLE dbo.Product (
ProductID int identity(1,1) primary key
, ProductName char(30)
, QtyOnHand int
, UnitPrice money
, InventoryListValue AS QtyOnHand * UnitPrice
)
GO

INSERT dbo.Product(ProductName, QtyOnHand, UnitPrice) SELECT 'Ipod', 25, 300
INSERT dbo.Product(ProductName, QtyOnHand, UnitPrice) SELECT 'Iphone', 99, 500
INSERT dbo.Product(ProductName, QtyOnHand, UnitPrice) SELECT 'Dell laptop', 10, 900
GO

SELECT * from dbo.Product
GO

 

 

The Best SQL Server 2005 Training in the World
 
 
SQLUSA.com Home Page