質問

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