The following snippets try to demonstrate some possibilities and pitfalls when creating tables with computed columns.
Table with a computed column
create table math (
val_1 decimal(6,2) not null,
op char(1) not null,
val_2 decimal(6,2) not null,
------
result as case op
when '+' then val_1 + val_2
when '-' then val_1 - val_2
when '*' then val_1 * val_2
when '/' then val_1 / val_2
end
);
insert into math values ( 3.2 , '+', 9.1 );
insert into math values (26.7 , '*', 13.5 );
insert into math values ( 4.62, '-', 7.7 );
insert into math values (81.3 , '/', 5.41);
Because the new inserted values cause the computed value to divide by zero, a select * from math will abort when the offending record is returned, the error message being Msg 8134, Level 16, … Divide by zero error encountered..