How do I INSERT in a table results from a separate queried table?

Posted on 2007-07-24
Last Modified: 2013-12-07
I'm trying to do a complex INSERT into a table. What makes it complicated is that I need to join two tables first, then insert the results of that table into a third table. Here's an example:

This is the query table:
SELECT tableA.ordid, tableA.custid,, tableB.custname
FROM tableA LEFT OUTER JOIN tableB ON tableA.custid = tableB.custid

This is the table I want to insert results of the query table into:
INSERT INTO tableC SET tableC.ordid = queryTable.ordid, =, tableC.custname = queryTable.custname

How do I do this with one statement if possible?
Question by:bemara57
    LVL 142

    Accepted Solution

    INSERT INTO tableC  (ordid, [date], custName )
    SELECT a.ordid, a.[date], b.custname
    FROM tableA a
    LEFT OUTER JOIN tableB b
      ON a.custid = b.custid
    LVL 75

    Assisted Solution

    by:Aneesh Retnakaran
    INSERT INTO tableC (ordId, Date,CustName)
    SELECT tableA.ordid, tableA.custid,, tableB.custname
    FROM tableA LEFT OUTER JOIN tableB ON tableA.custid = tableB.custid


    Author Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
    In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
    This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
    This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

    737 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

    15 Experts available now in Live!

    Get 1:1 Help Now