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

How to do the mutiple insert into..select query in a single insert statement?

我有一个 multiple insert into..select 语句如下。如果您看到,所有三个 statements.Everything 中只有一列值(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 returns 我有 20 行,首先我必须插入那 20 行 Product Manager 作为 [=32= 的值] 然后我必须插入相同的 20 行,但使用 Quality Manager 并再次使用 Deputy Engineer 。为此,我使用了 3 个插入语句。像这样我有 15 到 20 个插入语句。有没有一种方法可以在单个插入语句中做到这一点?它将帮助我减少代码并提高可读性。请提供替代解决方案。

由于 Req_Attrname 都是常量,您可以从 dualcross join 中查询所有这些结果,这样您就可以从查询匹配中获取每一行每个 Req_Attrname:

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',
           req_attrname,
           demo.attr_value
FROM       repo_tnr tnr
JOIN       repo_tnr_tttribute demo ON tnr.phyid = demo.phyid
CROSS JOIN (SELECT '**Product Manager**' AS req_attrname FROM dual
            UNION ALL
            SELECT '**Quality Manager**' AS req_attrname FROM dual
            UNION ALL
            SELECT '**Depty Engineer**'  AS req_attrname FROM dual) t
WHERE      demo.attr_name = 'Product Management Responsible' AND
           tnr.type = 'Complementary Item'