如何在单个insert语句中多次插入into..select查询?

时间:2015-07-17 07:11:26

标签: sql oracle oracle11g sql-insert

我正在多次插入to..select语句,如下所示。如果你看到,在所有三个语句中只有一个列值( Req_Attrname )正在发生变化。其他所有内容都相同,包括where条件。

插入声明1)

insert into details(repo_phyid,repo_type,repo_attrname,repo_Attrvalue
                    ,Req_phyid,Req_type,Req_Attrname,Req_attrvalue) 
select demo.phyid,'Complementary Item','Product Management Responsible',demo.attr_value,
                   tnr.phyid,'Complementary Item','**Product Manager**',demo.attr_value
       From repo_Tnr Tnr,repo_Tnr_Attribute demo 
       Where Tnr.Phyid=demo.Phyid 
             And demo.Attr_Name='Product Management Responsible'
             and tnr.type='Complementary Item'

插入声明2)

insert into details(repo_phyid,repo_type,repo_attrname,repo_Attrvalue,
                     Req_phyid,Req_type,Req_Attrname,Req_attrvalue) 
select demo.phyid,'Complementary Item','Product Management Responsible',demo.attr_value,
                   tnr.phyid,'Complementary Item','**Quality Manager**',demo.attr_value
       From repo_Tnr Tnr,repo_Tnr_Attribute demo 
       Where Tnr.Phyid=demo.Phyid 
             And demo.Attr_Name='Product Management Responsible'
             and tnr.type='Complementary Item'

插入声明3)

insert into details(repo_phyid,repo_type,repo_attrname,repo_Attrvalue
                    ,Req_phyid,Req_type,Req_Attrname,Req_attrvalue) 
select demo.phyid,'Complementary Item','Product Management Responsible',demo.attr_value
                    ,tnr.phyid,'Complementary Item',**'Depty Engineer**',demo.attr_value
        From repo_Tnr Tnr,repo_Tnr_Attribute demo 
        Where Tnr.Phyid=demo.Phyid 
              And demo.Attr_Name='Product Management Responsible'
              and tnr.type='Complementary Item'

我在这里尝试的是,如果select返回20行,首先我必须插入带有Product Manager的20行作为Req_attrname的值,然后我必须插入相同的20行但是{{1并且再次使用Quality Manager。为了完成这个我使用3插入语句。像这样我有15到20个插入语句。有没有办法可以在单个插入语句中执行此操作?它将帮助我减少代码并提高可读性。请提供替代解决方案。

1 个答案:

答案 0 :(得分:2)

由于Req_Attrname都是常量,因此您可以在dualcross join查询原始查询中的所有结果,以便从查询中获取每一行与每个{ {1}}:

Req_Attrname