Inserting from a SELECT but changing one column?

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

提交回复
热议问题