I have an Oracle database, and a table with several not null columns, all with default values.
I would like to use one insert statement for any data I want to insert, a
As explained in this AskTom thread, the DEFAULT
keyword will only work as a stand-alone expression in a column insert and won't work when mixed with functions or expressions such as NVL.
In other words this is a valid query:
INSERT INTO schema.my_table
( pk_column, other_column, not_null_column_with_default_value)
VALUES
(:pk_column,:other_column, DEFAULT)
You could use a dynamic query with all rows and either a bind variable or the constant DEFAULT if the variable is null. This could be as simple as replacing the string :not_null_column_with_default_value
with the string DEFAULT
in your $insert
.
You could also query the view ALL_TAB_COLUMNS
and use nvl(:your_variable, :column_default)
. The default value is the column DATA_DEFAULT
.