David Longfellow
David Longfellow

Reputation: 51

How to Insert Data from multiple tables into one

I am trying to insert data into a table using data from 2 other tables.

I need to use:

Project.ProjectID and Action.ActionID to be inserted into a table I have called

ActionDetails, WHERE the Project.ProjectID = 1

Any ideas...? i've googled everywhere with no success :(

Upvotes: 1

Views: 1620

Answers (2)

Andrew
Andrew

Reputation: 336

only if the table is not yet created it will create the table for you

select  P.ProjectId,                       
                A.ActionID 
        into [NEW_TABLE]                                                                                      
        from Project P 
        inner join [Action] A --jOINING THE TWO TABLES 
        WHERE P.ProjectID = 1 

Upvotes: 1

RichardTheKiwi
RichardTheKiwi

Reputation: 107696

You have actions from a template, that you need to add to ActionDetails after creating a new Project? Try this

Insert ActionDetails (ProjectID, ActionID)
select P.ProjectId, A.ActionID
from Project P
inner join Action A on A.templateID = 1  -- or some condition on A
WHERE P.ProjectID = 1

If the action table contains just a single list to be added to all projects, use a cross join instead

Insert ActionDetails (ProjectID, ActionID)
select P.ProjectId, A.ActionID
from Project P cross join Action A
WHERE P.ProjectID = 1

Upvotes: 1

Related Questions