I am very new to Oracle and Clob datatype.
We have a table with name ABC which has a column of CLOB data type.
The Clob Column "MyClob" has following text data:
{ "UD_1":"John", "UD_2":"Doe", "UD_3":"1011=John Doe"}
When I query the table 'ABC' using below query,
Select Id,
JSON_VALUE(MyClob, '$.UD_1') FirstName
JSON_VALUE(MyClob, '$.UD_2') LastName
JSON_VALUE(MyClob, '$.UD_3') Status
From Abc
I get the following table:
Id FirstName LastName Status
1 John Doe 1011=John Doe
2 Jack Ham 2022=Jack Ham
I want to update only the 'UD_3' value for one of the rows. I am not sure how to write an update query to update the text in Clob column.
You can perform updates to JSON using JSON_MERGEPATCH . To use JSON_MERGEPATCH
, in the second argument you will need to pass a JSON object of the key/value pairs that you want to update. See example below.
Another item to note is that the returned value of JSON_MERGEPATCH
will remove any unnecessary spaces from the JSON.
SQL> CREATE TABLE sampletable
2 (
3 id,
4 myclob
5 )
6 AS
7 SELECT 1,
8 EMPTY_CLOB ()
9 || '{ "UD_1":"John", "UD_2":"Doe", "UD_3":"1011=John Doe"}'
10 FROM DUAL
11 UNION ALL
12 SELECT 2,
13 EMPTY_CLOB ()
14 || '{ "UD_1":"Jack", "UD_2":"Ham", "UD_3":"2022=Jack Ham"}'
15* FROM DUAL;
Table SAMPLETABLE created.
SQL> SELECT *
2 FROM sampletable t
3 CROSS JOIN
4 JSON_TABLE (
5 t.myclob,
6 '$'
7 COLUMNS (firstname VARCHAR2 PATH '$.UD_1',
8 lastname VARCHAR2 PATH '$.UD_2',
9* status VARCHAR2 PATH '$.UD_3'));
ID MYCLOB FIRSTNAME LASTNAME STATUS
_____ _________________________________________________________ ____________ ___________ ________________
1 { "UD_1":"John", "UD_2":"Doe", "UD_3":"1011=John Doe"} John Doe 1011=John Doe
2 { "UD_1":"Jack", "UD_2":"Ham", "UD_3":"2022=Jack Ham"} Jack Ham 2022=Jack Ham
SQL> UPDATE sampletable
2 SET myclob = json_mergepatch (myclob, '{"UD_3":"3033-Devp"}')
3* WHERE id = 1;
1 row updated.
SQL> commit;
Commit complete.
SQL> SELECT *
2 FROM sampletable t
3 CROSS JOIN
4 JSON_TABLE (
5 t.myclob,
6 '$'
7 COLUMNS (firstname VARCHAR2 PATH '$.UD_1',
8 lastname VARCHAR2 PATH '$.UD_2',
9* status VARCHAR2 PATH '$.UD_3'));
ID MYCLOB FIRSTNAME LASTNAME STATUS
_____ _________________________________________________________ ____________ ___________ ________________
1 {"UD_1":"John","UD_2":"Doe","UD_3":"3033-Devp"} John Doe 3033-Devp
2 { "UD_1":"Jack", "UD_2":"Ham", "UD_3":"2022=Jack Ham"} Jack Ham 2022=Jack Ham
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.