Updating a column in one table from another table

  • We have two tables with names X and Y.

    X has a,b columns. And Y has c,d columns.

    I want to update b column in X table with the values from d column in Y table on condition X.a=Y.c.

    Can u help me with this query??

    Thanks,,

  • Check in Books Online for UPDATE, specifically UPDATE FROM. If you are still unsure, then take the time to read the posting etiquette article linked in my signature "please read this", and post DDL for your two tables with scripts to populate them with sample data.

    “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

  • UPDATE x SET x.b = y.d

    FROM TABLE1 x

    JOIN table2 y ON x.a = y.c

  • Just to be clear, Chris was trying to get the OP to teach themselves how to answer such simple questions on their own instead of making them permanently dependent on the kindness of others on the web. Books Online is a very good tool for learning such basics and more. I'm amazed at how many people that are using SQL Server that don't even know what Books Online is.

    @Gautham.gn,

    Two questions for you now that you have the coded answer to your question... 1) Do you understand all of the parts of that code and why it works and 2) do you know what Books Online actually is?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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