Puru
Puru

Reputation: 21

Extracting Data from CLOB data using SQL from Oracle Database

I have a table named "device" which contains a column named as "XMLdoc" of CLOB datatype. I want to update the Value field.

Name="DropDirectory" Value=""

/*Following is the sample XML:*/

<?xml version="1.0" encoding="UTF-8"?>
<Attributes>
    <Attribute DataType="Text-40" DisplayName="PrinterAlias"
        IsNotDeletable="Y" Modifiable="Y" Name="PrinterAlias" Value="QALABHP"/>
    <Attribute DisplayName="PrintServerHostName"
        Name="PrintServerHostName" Value="zzzzz"/>
    <Attribute DisplayName="PrintServerPort" Name="PrintServerPort" Value="2723"/>
    <Attribute DataType="Text-40" DisplayName="DropDirectory"
        IsNotDeletable="Y" Modifiable="Y" **Name="DropDirectory" Value=""/>
</Attributes>

Upvotes: 1

Views: 1437

Answers (2)

paulzip
paulzip

Reputation: 86

You didn't specify your Oracle version. I'll assume it's 11g.. (Note : If you are using 12c, you should probably use XQuery).

Here I update the Value attribute to new_value.

select xmlserialize(content updatexml(xmltype(
'<Attributes>
    <Attribute DataType="Text-40" DisplayName="PrinterAlias"
        IsNotDeletable="Y" Modifiable="Y" Name="PrinterAlias" Value="QALABHP"/>
    <Attribute DisplayName="PrintServerHostName"
        Name="PrintServerHostName" Value="zzzzz"/>
    <Attribute DisplayName="PrintServerPort" Name="PrintServerPort" Value="2723"/>
    <Attribute DataType="Text-40" DisplayName="DropDirectory"
        IsNotDeletable="Y" Modifiable="Y" Name="DropDirectory" Value=""/>
</Attributes>'), '/Attributes/Attribute[@Name="DropDirectory"]/@Value', 'new_value'))
from dual

Result (CLOB):

<Attributes>
   <Attribute DataType="Text-40" DisplayName="PrinterAlias" IsNotDeletable="Y" Modifiable="Y" Name="PrinterAlias" Value="QALABHP" />
   <Attribute DisplayName="PrintServerHostName" Name="PrintServerHostName" Value="zzzzz" />
   <Attribute DisplayName="PrintServerPort" Name="PrintServerPort" Value="2723" />
   <Attribute DataType="Text-40" DisplayName="DropDirectory" IsNotDeletable="Y" Modifiable="Y" Name="DropDirectory" Value="new_value" />
</Attributes>

Upvotes: 0

XING
XING

Reputation: 9886

You can update it as below:

Select:

SELECT extract(xmltype(col1), '/Attributes/Attribute[@Name="DropDirectory"]/@Value') 
  FROM test_clob;

Output:

 SQL> SELECT extract(xmltype(col1), '/Attributes/Attribute[@Name="DropDirectory"]/@Value') 
  FROM test_clob;    

EXTRACT(XMLTYPE(COL1),'/ATTRIBUTES/ATTRIBUTE[@NAME="DROPDIRECTORY"]/@VALUE')
--------------------------------------------------------------------------------

Update:

 UPDATE test_clob 
  SET col1 =   UPDATEXML(xmltype(col1),
   '/Attributes/Attribute[@Name="DropDirectory"]/@Value',to_char('google.com')).getClobVal()

Output:

 SQL> /

EXTRACT(XMLTYPE(COL1),'/ATTRIBUTES/ATTRIBUTE[@NAME="DROPDIRECTORY"]/@VALUE')
--------------------------------------------------------------------------------
google.com

Note: Replace your tablename and columnname with mine.

Upvotes: 1

Related Questions