如何为这个Oracle查询应用一种循环?

时间:2013-02-22 00:26:52

标签: sql oracle oracle11g

我有一个使用Oracle开发的查询。我想更新同一列

' 5'倍。在我开发的查询下面:

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' 

此查询中的挑战是列&#39; pr.attribute_de_de&#39;应该更新sellpoint1,sellpoint2,sellpoint3,sellpoint4,sellingpoint5。如何在oracle中完成此操作。非常感谢您的任何建议

2 个答案:

答案 0 :(得分:2)

您不需要循环,因为Oracle updateXML函数可用于在单个SQL UPDATE语句中的多个节点上用新值替换现有元素,属性和其他节点。

...    
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')  
...

查看XMLtype operations的Oracle文档。

答案 1 :(得分:0)

我认为您的“USING”查询中需要五行。 UNION会工作吗?说,像这样:

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 ...

...然后是你的查询的其余部分,但是使用“sellpoint”代替“sellpoint1”,“sellingpoint2”等等。

注意UNION ALL而不是UNION:plain UNION(没有ALL)将消除重复的行。我假设你每次都想要五行,无论重复是什么。

希望这至少是正确方向的推动。我得到了所有睡眼惺with的XML查询:)