I have a column in my table called Value1. I then have a computed column, Value2, with the formula of;
(CASE WHEN [Value1] > [Value2] THEN [Value1] ELSE [Value2] END)
I can’t save this as SQL Server balks at the self-reference of the Value2 computed column in the formula.
Is there anything else I can do?
It seems like you need to keep track of not only what value1 is now but also what value1 used to be. You won’t be able to do that with a computed column, because it can only react to the current value, not itself or the previous value.
I suggest an
INSTEAD OF TRIGGERas opposed to a computed column. Here is a simple example:An
INSTEAD OF INSERT TRIGGER:An
INSTEAD OF UPDATE TRIGGER:Now let’s insert a couple of rows and prove we can maintain Value2 without ever inserting or updating that column directly:
Clean-up: