2013-02-22 22 views
0

我有一個使用Oracle開發的查詢。我想更新同一列如何爲這個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' 

在此查詢的挑戰是列 'pr.attribute_de_de' 應該sellingpoint1,sellingpoint2,sellingpoint3,sellingpoint4,sellingpoint5更新。在oracle中如何做到這一點。非常感謝你的任何建議

回答

0

我認爲你需要在你的「USING」查詢中有五行。聯盟會工作嗎?再說,這樣的事情:

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

...然後在您的查詢的休息,但使用 「sellingpoint」 而不是 「sellingpoint1」, 「sellingpoint2」 等

注意UNION ALL代替UNION:普通UNION(沒有ALL)將消除重複的行。我假設你每次都需要五行,而不管重複。

希望這是至少在正確的方向推動。我得到所有關於XML查詢的漂亮工作:)

+0

非常感謝你的回覆,我不想每次更新五行,我想爲(int i = 0; i <5; i ++){'sellingpoint'+ i}做一些這樣的事情,這樣它生成sellingpoint1,sellingpoint2,....我正在尋找甲骨文解決方案 – user964147 2013-02-22 00:59:19

+0

對不起,我誤解了你的問題。恐怕我不是100%(當我需要它的時候我會重新學習它,這種情況並不常見,然後我會在一段時間後忘記它),所以希望對MERGE更熟悉的人可以跳轉在。 – 2013-02-22 01:15:11

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文檔。

相關問題