Insert value Into TableA From TableB Where multiple fields match in both

Posted on 2014-08-06
Last Modified: 2014-08-06
Hopefully the title sums it up but:


I'm working in SQL Server 2008.  

I need to get the value from col1 in Table_A and insert it into col1 in Table_B where Table_A.col2 = Table_B.col2, and TableA.col3 = Table_B.col3, and Table_A.col4 = TableB.col4

The relationship is one to many Table_A to Table_B.

Hopefully that makes enough sense.  

Thanks in advance!!!
Question by:ttist25
    LVL 32

    Accepted Solution

    This is not an INSERT, it's an UPDATE. It's almost like your description:

    SET     col1 = A.col1
    FROM    Table_A A
            INNER JOIN Table_B B ON A.col2 = B.col2
                                    AND A.col3 = B.col3
                                    AND A.col4 = B.col4;

    Open in new window

    LVL 1

    Author Comment

    Worked a treat!  Thank you sir!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
    Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now