Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2011
    Posts
    5

    Unanswered: Insert data into a table from another table

    I have two tables as follow:

    HTML Code:
    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.

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    UPDATE sales
       SET sales.address1 = t2.address1
         , sales.address2 = t2.address2
      FROM sales t1
    INNER
      JOIN addresses t2
        ON t1.addressid = t2.addressid
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •