Inserting from a SELECT but changing one column?

前端 未结 6 763
忘了有多久
忘了有多久 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 02:53

    using Oracle

    DECLARE
    CURSOR CUR_D IS
      SELECT *
      FROM TABLE
      WHERE id = some id;
    ROW_D CUR_D%ROWTYPE;
    
    BEGIN
      OPEN CUR_D;
      FETCH CUR_D INTO ROW_D;
      CLOSE CUR_D;
    
      ROW_D.column := 'some data';
    
      INSERT INTO some table
      VALUES ROW_D;
    
    END;
    /
    
    0 讨论(0)
  • 2021-02-05 02:58

    zim RDBMS will allow it. All fields matching by fieldname will obtain values from the source, unless overridden by a let statement. eg.

        `add all details from details where account_month = 20190101 let level = 2 increase = base * 1.1 total = increase * amount`
    

    This would for all detail records for the specified accounting month, add a level 2 record with all field values matching the source set, except these assigned/calculated amounts: level is 2. increase is original base times 1.1. total is new increase (original base times 1.1) times the amount. If you switch the order of assignment of increase and total, then total will use the source/original increase in the calculation.

    0 讨论(0)
  • 2021-02-05 03:07

    You can use temporary tables

    Create Temporary table

    CREATE TEMPORARY TABLE temp_table AS (SELECT * FROM MyTable WHERE ...);
    

    Update column

    UPDATE temp_table SET column='Value' WHERE ...;
    

    Or drop a column

    ALTER TABLE temp_table DROP column_name;
    

    Insert to destination table

    INSERT INTO MyDestinationTable SELECT * FROM temp_table;
    
    0 讨论(0)
  • 2021-02-05 03:10

    For SQL Server, the syntax would be:

    insert  into TargetTable
            (col1, col2, col3, ...)
    select  getdate(), col2, col3, ... 
    from    SourceTable
    

    SQL Server can generate a column list for you. Right click the table name, and choose Script as -> Select -> To New Window.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-02-05 03:15

    Create a VIEW with required number for columns.

    Assume Tbl1 has 4 columns. Create a view with required columns. Here Col1 has been excluded.

    CREATE VIEW V1 AS
    SELECT col2, col3, col4
    FROM TBL1
    

    Use the VIEW for Inserting. Col1 value for TBL2 will be current date, for other columns, the values will be from view V1 ie., col2, col3, col4 from TBL1

    INSERT INTO TBL2
    SELECT GETDATE(), * 
    FROM V1
    

    This way you dont need to specify all the columns each time.

    Hope it helps

    0 讨论(0)
提交回复
热议问题