Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting multiple rows into a table using data from 2 other tables

Status
Not open for further replies.

wallaceoc80

Programmer
Jul 7, 2004
182
GB
I have a table that is a relation table that is used in resolving a many-to-many relationship.

The table Group_App has columns (GroupID(pk), AppID(pk))

It resolves the relation ship between:

Group(GroupID(pk), GroupName)

and

Application(AppID(pk), AppName)

What I want to do is run a query that will fill the Group_App table with all the Applications from the Application table and for each AppID will have GroupID of 4.

So for example the Group_App table will have the following column pairings:

GroupID AppID
4 1
4 2
4 3
...

Can anybody tell me is it possible to write a query that will do this?

Thanks for the help!

Wallace
 
Code:
insert into Group_App (GroupID, AppID)
  select 4 as GroupID, AppID
    from Application
    where Application.AppID not in
              (select AppID from Group where GroupID=4)
 
You can use CROSS JOIN.

INSERT INTO Group_App (GroupID, AppID)
SELECT G.GroupID, A.AppID
FROM Group G CROSS JOIN Application A



Andel
andel@barroga.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top