Inserting from a SELECT but changing one column?

前端 未结 6 768
忘了有多久
忘了有多久 2021-02-05 02:35

Wondering if there is a way to insert a row into a table from another, with exception of one column?

This is of course easy with a limitied amount of columns, but gets k

6条回答
  •  情深已故
    2021-02-05 03:13

    assuming that your select has attributes uniquely defining the result set wrt to the contents of your target table before insertion, you can apply the following 2 steps:

     Insert into target_table
     select *
      from source_table
     where yada yada yada
       and characteristic_yada
         ;
    
    update target_table
       set col1 = current date
     where characteristic_yada
         ;
    
     commit;
    

    make sure to issue both commands inside the same transaction as shown. also be aware that characteristic_yada must be aplicable to source and target table alike and that the suitability of characteristic_yada needs to be checked before each application of the statements unless they refer to pks/aks of the taregt table

提交回复
热议问题