Insert default value when null is inserted

后端 未结 5 1389
有刺的猬
有刺的猬 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:16

    I think the cleanest way is to not mention them in your INSERT-statement. You could start writing triggers to fill default values but that's heavy armor for what you're aiming at.

    Isn't it possible to restructure your application code a bit? In PHP, you could construct a clean INSERT-statement without messy if's, e.g. like this:

     $value) {
      if (is_null($value) || $value=="") {
        unset($insert[$key]);
      }
    }
    
    // construct insert statement
    $statement = "insert into table (". implode(array_keys($insert), ',') .") values (:". implode(array_keys($insert), ',:') .")";
    
    // call oci_parse
    $stid = oci_parse($conn, $statement);
    
    // bind parameters
    foreach ($insert as $key => $value) {
      oci_bind_by_name($stid, ":".$key, $value);
    }
    
    // execute!
    oci_execute($stid);
    ?>
    

提交回复
热议问题