PLSQL CLOBS into variables

DaveQuinn picture DaveQuinn · Sep 17, 2012 · Viewed 14.7k times · Source

I'm trying to save a CLOB into a variable to perform operations like extract and such. I have this code:

DECLARE
  clob_rec CLOB;
  n_rec NUMBER:=100;
BEGIN
  SELECT LOB INTO clob_rec FROM table1 WHERE ID = 1234;
  n_rec := clob_rec.EXTRACT('//XPTO/text()', 'xmlns:XPTO').getNumVal();
END;

I want to save multiple values from the XML to various variables like n_rec. How can get an "instance of the object (CLOB)" to perform functions or methods like extract()?

Answer

Jeffrey Kemp picture Jeffrey Kemp · Sep 18, 2012

You need to convert it to an XMLtype first:

DECLARE
  clob_rec CLOB;
  n_rec NUMBER:=100;
  x XMLType;
BEGIN
  SELECT LOB INTO clob_rec FROM table1 WHERE ID = 1234;
  x := XMLType(clob_rec);
  n_rec := x.EXTRACT('//XPTO/text()', 'xmlns:XPTO').getNumVal();
END;