문제

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 )

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 dba.stackexchange
scroll top