Insert Statement.. quick tutorial

  • Hi .. kindly help

    Need a little quick tutorial... I din't get my statement below right..

    Insert into tb1 (port)

    Select port From tb2

    where tb1.ship_id= tb2.ship_id

    I want to insert port into tb1 from tb2 where tb1.ship_id = tb2.ship_id. kindly help.

  • What you're asking doesn't make sense. Do youi want to add new records to tb1, or do you want to update fields of existing records in tb1 to a value in tb2?

    If the latter, UPDATE tb1 SET Port = tb2.Port FROM tb2 WHERE tb1.ship_id= tb2.ship_id

    If the former, please explain clearer exacly what you want.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Hi There,

     -- What you need to do  for an insert is the following:

    insert into tbl1

    (port)

    Select tbl2.port from tbl2 INNER JOIN tbl1 ON tbl1.ship_id = tbl2.ship_id

    -- If it is an update then you need the following:

    update tbl1

    set port = tbl2.port from tbl2 INNER JOIN tbl1 ON tbl1.ship_id = tbl2.ship_id

     

    Cheers,

    Chris

    Chris

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply