How to set a conditional DEFAULT value to a column in oracle without using triggers? I want to achieve following needs:
- If "flag1"=1 then Default value to column Newfield must be "4".
- If "flag1"=2 then Default value to column Newfield must be "5".
How to set a conditional DEFAULT value to a column in oracle without using triggers? I want to achieve following needs:
If 'flag' is something session (or statement) level then SYS_CONTEXT may be a way through. Possibly a BEFORE STATEMENT trigger if the issue is avoiding a context switch for every row inserted
If column
Newfield
does not need to be updateable, then you could simply implement it in a view, as Tony showed, or in 11g you could make it a virtual column.That's what triggers are for. Use them.
A column DEFAULT is not allowed to reference another column, so this is not possible.
You could perhaps just let the default remain as NULL, and then have a view to adjust it like: