0

Let's say , I have two table with same schema but different data .
Table_A and Table_B .

Table_A
--------
ID(p_key)     Number(p_key)         Column3     Column4
-----------------------------------------------------
ID1                1                  AAA         BBB
ID1                2                  CCC         DDD
ID2                1                  EEE         FFF
ID2                2                  GGG         HHH

-

Table_B
--------
ID(p_key)     Number(p_key)         Column3     Column4
-----------------------------------------------------
ID1                1                  AAA_1       BBB_1
ID1                2                  CCC_1       DDD_1
ID2                1                  EEE_1       FFF_1
ID2                2                  GGG_1       HHH_1

I want to export(overwrite) Table_B column3 data to Table_A column3 , where ID and Number Columns data are equal .
After executing of script , Table_A's data should be ,

 Table_A
    --------
    ID(p_key)     Number(p_key)         Column3     Column4
    -----------------------------------------------------
    ID1                1                  AAA_1        BBB
    ID1                2                  CCC_1        DDD
    ID2                1                  EEE_1        FFF
    ID2                2                  GGG_1        HHH

How can I make this using sql script only ?
I use MS SQL-Server 2008 R2 .

1 Answer 1

1
UPDATE TBLA
    SET TBLA.Column3=TBLB.Column3 --, TBLA.Column4=TBLB.Column4 if you want
FROM
    Table_A AS TBLA
    LEFT OUTER JOIN Table_B AS TBLB ON (TBLB.ID1 = TBLA.ID1 AND TBLB.ID2 = TBLA.ID2)

Please note that 'ID' columns (i.e. 'primary keys') must be unique (as pkeys are :).
But to be sure -as I don't know your exact table structure- before you execute the code above, create a SELECT statement with the join(s) and if the result set is correct, then add it to the UPDATE.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.