Insert default value when null is inserted

后端 未结 5 1231
有刺的猬
有刺的猬 2021-02-13 21:07

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

5条回答
  •  花落未央
    2021-02-13 21:24

    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.

提交回复
热议问题