locked
Insert into column of the same name from different table RRS feed

  • Question

  • User-2006371901 posted

    I have two tables that I want to mirror the contents of table 1's bikemfg column into table 2's bikemfg column on an insert

    CREATE TABLE [dbo].[bike](
    bikeid [int] IDENTITY(1,1) NOT NULL,
    bikemfg [varchar] (15) null
    )

    CREATE TABLE [dbo].[bikemodel](
    bmid [int] IDENTITY(1,1) NOT NULL,
    bikeid [int] null,
    bikemodelname [varchar] (25) null,
    bikemfg [varchar] (15) null
    )
    -- the query I'm trying
    Insert into bikemodel (bikemfg)
    select bikemfg from bike 

    I want to insert bikemfg data that corresponds with bikemodelname of each manufacturer, referenced by bikeid
    ..but get empty results when using similar statement above

    thanks in advance
    Ned

    Thursday, October 3, 2019 11:02 PM

Answers

  • User288213138 posted

    Hi Norkel,

    Norkle

    I have two tables that I want to mirror the contents of table 1's bikemfg column into table 2's bikemfg column on an insert

    If you want to insert the data of bike.bikemfg into bikemodel.bikemfg, then I suggest you use the update statement.

    Note: I tested in sql database.

    update bikemodel set bikemodel.bikemfg=bike.bikemfg from bike where bikemodel.bikeid=bike.bikeid

    My data:

        

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 4, 2019 3:14 AM