Xquery to update value of a node of a xml stored in a column of a table

1.3k views Asked by At

i use DB2 express-c edition v9.1 database management system.

name of the table: student
name of the column: course

xml in the column:

<ids>
<course>
<id>

**101**  

</id> 
<lecture-id>

**0**  
</lecture-id>

</course>
<course>
<id>

**102**  

</id>
<lecture-id>

**2**  

</lecture-id>
</course>
</ids>

I need to update the value of <lecture-id> node of <course> node with <id> node as 101, to 1.
how should I query the database to get the desired result.
any help would be highly appreciated.

1

There are 1 answers

1
Jalew On

I think it can be like this:

    UPDATE student
       SET course = XMLQUERY('transform 
                            copy      $new := $i
                            modify    do replace value of $new/ids/course/lecture-id
                                                       with 1
                            return    $new' PASSING course AS "i")

 WHERE id = 101;