Use An Aggregate With Computed Column
-
07-10-2020 - |
문제
I am wanting to alter my table to have a column that is the SUM() of two other fields. I have this syntax
ALTER TABLE SalesData
ADD AC AS SUM(COALESCE(val1,0)+COALESCE(val2,0))
However it produces an error of
An aggregate may not appear in a computed column expression or check constraint.
What is the proper way to add a field and set the fields value to a calculation?
해결책
I'd not use SUM() for this. Just code
ALTER TABLE .. ADD .. AS (col1 + col2);
(see also: https://docs.microsoft.com/en-us/sql/relational-databases/tables/specify-computed-columns-in-a-table )
제휴하지 않습니다 dba.stackexchange