开发者

How to Insert Data from multiple tables into one

I am trying开发者_如何学C 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 :(


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


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 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜