• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

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

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, tableA.date, 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.date = queryTable.date, tableC.custname = queryTable.custname

How do I do this with one statement if possible?
0
bemara57
Asked:
bemara57
2 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
INSERT INTO tableC (ordId, Date,CustName)
SELECT tableA.ordid, tableA.custid, tableA.date, tableB.custname
FROM tableA LEFT OUTER JOIN tableB ON tableA.custid = tableB.custid

0
 
bemara57Author Commented:
Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now