'Can I make auto sum in SQL Server?
I want sum single row in last column in same row like
-------------------------------
Value 1 Value 2 Sum
-------------------------------
2 5 ?
5 10 ?
-------------------------------
I want to sum this values automatically
I tried to add (Sum(Value1 + Value2)) in default value for Sum
Solution 1:[1]
You can do it like this
CREATE TABLE [dbo].[Test]
(
[Value 1] [INT] NULL,
[Value 2] [INT] NULL,
[Value 3] AS ([Value 1] + [Value 2])
) ON [PRIMARY]
GO
INSERT INTO dbo.Test ([Value 1], [Value 2])
VALUES (1, 2)
SELECT *
FROM dbo.Test
Results:
Value 1 Value 2 Value 3
1 2 3
Solution 2:[2]
It goes like this if you all ready have table drop the column of that total then:
ALTER TABLE [TableName] ADD [ColumnName] AS ([Column1] * [Column2])
use * or + or - as you want
If you will create new table, the code will be like this:
CREATE TABLE [TableName] (Col1 int, Col2 int , Col3 as ([col1] * [col2])) ;
use * or + or - as you want
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | marc_s |
| Solution 2 | Jeremy Caney |
