Add value from two columns as the computed column : Computed Column « Table « SQL Server / T-SQL






Add value from two columns as the computed column


1>
2> CREATE TABLE T (
3>     int1 int,
4>     bit1 bit,
5>     varchar1 varchar(3),
6>     dec1 dec(5,2),
7>     cmp1 AS (int1 + bit1)
8> )
9> GO
1> INSERT T (int1, bit1) VALUES (1, 0)
2> GO

(1 rows affected)
1> INSERT T (int1, varchar1) VALUES (2, 'abc')
2> GO

(1 rows affected)
1> INSERT T (int1, dec1) VALUES (3, 5.25)
2> GO

(1 rows affected)
1> INSERT T (bit1, dec1) VALUES (1, 9.75)
2> GO

(1 rows affected)
1> --Average of dec1 values
2> SELECT CAST(AVG(dec1) AS dec(5,2)) 'Avg of dec1'
3> FROM T
4> WHERE dec1 IS NOT NULL
5> GO
Avg of dec1
-----------
       7.50

(1 rows affected)
1>
2>
3> drop table t
4> GO
1>
           
       








Related examples in the same category

1.Computed columns are (by default) virtual columns not physically stored in the table