locked
Insert data into a table from another table RRS feed

  • Question

  • Not sure if I am in the right forum.

    I have two tables as follow:


    Table: Addresses

    AddressID Address1 Address2
    1 123 West St Santa Ana, CA 92703
    2 24 Cantera New York, NY 10017
    5 32 Clinton Fountain, CA 92708
    7 124 Edwards Las Vegas, NV 25478
    10 417 Fontana Irvine, CA 92478

    Table: Sales

    SaleID AddressID Address1 Address2
    21 2    
    22 5    
    23 7    

    I want to fill the Address1 and Address2 columns of the Sales table with the matching AddressID from the Addresses table. What is the SQL syntax? I am using SQL Server 2005. Thanks.



    Sunday, March 20, 2011 3:22 AM

Answers

  • You need to use UPDATE operator.

    UPDATE S
    
    SET Address1 = A.Address1,
    
          Address2 = A.Address2
    
    FROM Sales S
    
    INNER JOIN Addresses A ON S.AddressID = A.AddressID
    

    For every expert, there is an equal and opposite expert. - Becker's Law

    Naomi Nosonovsky, Sr. Programmer-Analyst

    My blog


    • Marked as answer by KJian_ Thursday, March 24, 2011 8:30 AM
    Sunday, March 20, 2011 4:09 AM
  • UPDATE Sales
    SET Address1 = B.Address1, Address2 = B.Address2
    FROM Sales
    INNER JOIN Addresses B ON Sales.AddressID = B.AddressID
    

    Vishal Gajjar
    • Marked as answer by KJian_ Thursday, March 24, 2011 8:30 AM
    Sunday, March 20, 2011 4:09 AM

All replies

  • You need to use UPDATE operator.

    UPDATE S
    
    SET Address1 = A.Address1,
    
          Address2 = A.Address2
    
    FROM Sales S
    
    INNER JOIN Addresses A ON S.AddressID = A.AddressID
    

    For every expert, there is an equal and opposite expert. - Becker's Law

    Naomi Nosonovsky, Sr. Programmer-Analyst

    My blog


    • Marked as answer by KJian_ Thursday, March 24, 2011 8:30 AM
    Sunday, March 20, 2011 4:09 AM
  • UPDATE Sales
    SET Address1 = B.Address1, Address2 = B.Address2
    FROM Sales
    INNER JOIN Addresses B ON Sales.AddressID = B.AddressID
    

    Vishal Gajjar
    • Marked as answer by KJian_ Thursday, March 24, 2011 8:30 AM
    Sunday, March 20, 2011 4:09 AM