How to update joined tables

  • Hello clever peeps

    Does anyone know how to update using a joined set of tables

    I have two tables with a common primary key. Table compound has the following fields

    compound_id (int primary key)

    compound (varchar(100)

    hazard_info (varchar(200)

    Table hazard has the following fields

    compound_id (int primary key matches other table)

    hazard_phrase (varchar(200)

    I would like to update the compound table and insert the hazard_phrase from the hazard table into the hazard_info of the compound table.

    I am using SQL Server 8

    Thanks in advance

  • See BOL ---> UPDATE statement ---> Using the UPDATE statement with information from another table

  • UPDATE c SET hazard_info = h.hazard_phrase

    FROM compound c

    INNER JOIN hazard h on h.compound_id = c.compound_id

    Cheers

    ChrisM

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Thank you

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

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