Link to home
Start Free TrialLog in
Avatar of Fairfield
FairfieldFlag for United States of America

asked on

SQL recursive insert

I have two tables.

Table1 looks like this:

PartNumber
123456-001
122334-002
22334455-003

Table2 looks like this:

Country
AA
BB
CC
DD
EE

I need a query that will take each line of Table1 and insert into new table (Table3) by taking each line from Table1 and use the sequence from Table2.

Table3 should look like this:

PartNumber            Country
123456-001             AA
123456-001             BB
123456-001             CC
123456-001             DD
123456-001             EE
122334-002             AA
122334-002             BB
122334-002             CC
122334-002             DD
122334-002             EE
.....etc.

Can someone please provide a query that will provide the resulting table?
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

This is untested but a simple Cartesian join should work:
insert into table3 (select partnumber, country from table1, table2);
ASKER CERTIFIED SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
FYI:
The CARTESIAN JOIN or CROSS JOIN returns the Cartesian product of the sets of records from two or more joined tables. Thus, it equates to an inner join where the join-condition always evaluates to either True or where the join-condition is absent from the statement.
https://www.tutorialspoint.com/sql/sql-cartesian-joins.htm