How do I use a computed column specification to persist a datetime value using getdate()?
Does anyone know if there is a way to achieve the same affect as a datetime
column with a default binding of getdate()
with computed columns?
I have 开发者_运维技巧tried setting the formula to getdate()
and persist to Yes
but I get an error
Computed column 'InsertDateTime' in table 'Tmp_Table' cannot be persisted because the column is non-deterministic.
forget the "computed column" and make it a regular not null column, with a default to GETDATE()
, or use an INSTEAD OF UPDATE/INSERT
trigger to set it.
you can't make a computed column use a function that constantly returns a different value (based on the same parameter values), it must return the same value each time (based on the same parameter values). Read this: Deterministic and Nondeterministic Functions
All functions are deterministic or nondeterministic:
- Deterministic functions always return the same result any time they are called with a specific set of input values.
- Nondeterministic functions may return different results each time they are called with a specific set of input values.
Whether a function is deterministic or nondeterministic is called the determinism of the function.
For example, the DATEADD built-in function is deterministic because it always returns the same result for any given set of argument values for its three parameters. GETDATE is not deterministic because it is always invoked with the same argument, yet the value it returns changes each time it is executed.
精彩评论