user964147
user964147

Reputation: 739

How to apply kind of loop for this Oracle query?

I have a query that was developed using Oracle. I want update the same column

'5' times. below the query that i developed:

MERGE INTO product pr 
USING(
SELECT pr.uuid,
            xmltype(pr.attributes_de_de).extract('//attr[@name = "SellingPoint1"]/string/text()')  AS sellingpoint1,
            xmltype(pr.attributes_de_de).extract('//attr[@name = "SellingPoint2"]/string/text()')  AS sellingpoint2,
            xmltype(pr.attributes_de_de).extract('//attr[@name = "SellingPoint3"]/string/text()')  AS sellingpoint3,
            xmltype(pr.attributes_de_de).extract('//attr[@name = "SellingPoint4"]/string/text()')  AS sellingpoint4,
            xmltype(pr.attributes_de_de).extract('//attr[@name = "SellingPoint5"]/string/text()')  AS sellingpoint5
  FROM product pr WHERE pr.defaultproductvariationid ='1tap_vEBvuEAAAE89CgjnPbb' AND pr.typecode = '16'
) defaultproducts ON (pr.uuid = '8d2p_vEBCJgAAAE8ruYjnPba')
WHEN MATCHED THEN 
UPDATE SET pr.attributes_de_de = CASE WHEN sellingpoint1 IS NOT NULL THEN
                                  CASE WHEN (SELECT count(1) existscount FROM product pr 
                                              WHERE pr.uuid = '8d2p_vEBCJgAAAE8ruYjnPba' 
                                              AND existsNode(xmltype(pr.attributes_de_de), '/attrs/attr[@name="SellingPoint1"]') = 1) = 1 
                                        THEN 
                                  UPDATEXML(XMLTYPE.createXML(pr.attributes_de_de),'/attrs/attr[@name = "SellingPoint1"]/string/text()', 
                                                    sellingpoint1).getClobVal() 
                                        ELSE 
                                  APPENDCHILDXML(xmltype(pr.attributes_de_de), 'attrs/attr[@name="SellingPoint22"]',
                                                    XMLType('<string>test</string>')).getClobVal()
                                        END  
                                    ELSE 
                                  DELETEXML(xmltype(pr.attributes_de_de), '/attrs/attr[@name="SellingPoint1"]').getClobVal()  
                                END
DELETE where pr.uuid != '8d2p_vEBCJgAAAE8ruYjnPba' 

the challenge in this query is the column 'pr.attribute_de_de' should update for sellingpoint1, sellingpoint2, sellingpoint3, sellingpoint4, sellingpoint5. How this can be done in oracle. Thank you very much for any suggestions

Upvotes: 0

Views: 1102

Answers (2)

gm2
gm2

Reputation: 144

You shouldn't need a loop because the Oracle updateXML function can be used to replace existing elements, attributes, and other nodes with new values at multiple nodes in a single SQL UPDATE statement.

...    
UPDATE SET pr.attributes_de_de = updateXML(pr.attributes_de_de, '/attrs/attr[@name = "SellingPoint1"]/string/text()', 'NewVal_SellingPoint1',  
                                                                '/attrs/attr[@name = "SellingPoint2"]/string/text()', 'NewVal_SellingPoint2',  
                                                                '/attrs/attr[@name = "SellingPoint3"]/string/text()', 'NewVal_SellingPoint3')  
...

Have a look at Oracle documentation for XMLtype operations.

Upvotes: 2

Ed Gibbs
Ed Gibbs

Reputation: 26343

I think you'll need five rows in your "USING" query. Would a UNION work? Say, something like this:

MERGE INTO product pr 
USING(
  SELECT pr.uuid, xmltype(pr.attributes_de_de).extract('//attr[@name = "SellingPoint1"]/string/text()') as sellingpoint,
  UNION ALL SELECT pr.uuid, xmltype(pr.attributes_de_de).extract('//attr[@name = "SellingPoint2"]/string/text()'),
  UNION ALL SELECT pr.uuid, xmltype(pr.attributes_de_de).extract('//attr[@name = "SellingPoint3"]/string/text()'),
  UNION ALL SELECT pr.uuid, xmltype(pr.attributes_de_de).extract('//attr[@name = "SellingPoint4"]/string/text()'),
  UNION ALL SELECT pr.uuid, xmltype(pr.attributes_de_de).extract('//attr[@name = "SellingPoint5"]/string/text()')
) defaultproducts ...

... and then the rest of your query, but using "sellingpoint" instead of "sellingpoint1", "sellingpoint2", etc.

Note the UNION ALL instead of UNION: plain UNION (without the ALL) will eliminate duplicate rows. I'm assuming you want five rows every time, regardless of duplicates.

Hope this is at least a nudge in the right direction. I get all bleary-eyed working with XML queries :)

Upvotes: 0

Related Questions