Kung Fu Ninja
Kung Fu Ninja

Reputation: 3752

oracle default value for a column

You know how we could use:

dateStamp DATE DEFAULT sysdate

to assign a default value to a column in table_x. What if I want to assign a default function? Can I do that?

The function will have some values from "table_params" to run some formula including a column named : "base" in table_x.

I could possibly write a cursor to loop through and run an update statement, but I was just curious if this is possible. thanks in advance.

Upvotes: 1

Views: 2354

Answers (2)

phil
phil

Reputation: 707

From Oracle documentation:

Restriction on Default Column Values
A DEFAULT expression cannot contain references to PL/SQL functions or to other columns, the pseudocolumns CURRVAL, NEXTVAL, LEVEL, PRIOR, and ROWNUM, or date constants that are not fully specified.

Either use a trigger (as was already mentioned) or run an UPDATE statement after your INSERT statement(s) (shouldn't be a problem if you keep your DML in PL/SQL).

Upvotes: 3

Lost in Alabama
Lost in Alabama

Reputation: 1653

You can write an INSERT trigger for the table that calls the function you want.

Upvotes: 2

Related Questions