2

I'm having a multiple insert into..select statement as below. If u see, there is only one column value(Req_Attrname) is changing in all the three statements.Everything else is same including the where conditions.

insert statement 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'

insert statement 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'

insert statement 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'

What i'm trying here is, if the select returns me 20 rows, first i have to insert that 20 rows with Product Manager as value for Req_attrname then i have to insert same 20 rows but with Quality Manager and again with Deputy Engineer . to accomplish this i'm using 3 insert statement. Like this i'm having 15 to 20 insert statement. Is there a way that can do it in single insert statement? it will help me to decrease the code and increase the readbility. Kindly provide an alternate solution.

1 Answer 1

2

Since the Req_Attrname are all constants, you can query all of them from dual and cross join this result on the original query so you get each row from your query matches with each 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'
2
  • Thanks it worked. :) I have one more doubt. What if those are not constants and i need to pick from some table ? Because i have those scenerio's also. Please help me. Commented Jul 17, 2015 at 7:37
  • @Crazy2crack you can just query that table instead of dual.
    – Mureinik
    Commented Jul 17, 2015 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.