MS SQL 2005 - set sum of three columns as default value
In MS SQL Server 2005, using SQL Server Management Studio, when creating/editing/designing a new table, is it possible to set a column default value to be the sum of 2 or 3 other columns in the same table?
Let's say I have a table where most of the columns are smallint datatypes.
How can I set the default value of "Total" to be the sum of ProdA, ProdB, and ProdC?
ID Month Year ProdA ProdB ProdC Total
1 Oct 2010 44 22 11 77
2 Nov 2010 22 11 1 34
Have a look at Computed columns.
Create Table t1 (
[ID] int identity (1, 1) NOT NULL,
[Month] char(3) NOT NULL,
[YEAR] smallint NOT NULL,
[ProdA] int NOT NULL,
[ProdB] int NOT NULL,
[ProdC] int NOT NULL,
[Total] as (ProdA + ProdB + ProdC)
Awesome! Thanks, russell.
Will the same format work if I'm creating the table directly in Server Management Studio? I'll give that a shot.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)