27 settembre 2008

Inserire un CLOB

Come alcuni sapranno, in una pl/sql il numero massimo di caratteri consentiti per una variabile è di 32.000 e questo può risultare un problema quando ad esempio si esporta una riga contenente un CLOB e si vuole reinserire il contenuto del campo di tipo clob mediante pl/sql.
Per questo ho escogitato un workaround non flessibilissimo, ma che funziona.



DECLARE
lob_loc CLOB;
amount INTEGER;
writeAmount BINARY_INTEGER;
intermediaField CLOB;
newData CLOB;
primaParte VARCHAR2(4000);
lunPrimaParte INTEGER;
secondaParte VARCHAR2(4000);
lunSecondaParte INTEGER;
terzaParte VARCHAR2(4000);
lunTerzaParte INTEGER;
BEGIN

DBMS_LOB.createtemporary(intermediaField, true);
primaParte := 'testo prima parte';
secondaParte := 'testo seconda parte';
terzaParte:= 'testo terza parte';
lunPrimaParte := length(primaParte);
DBMS_LOB.write(intermediaField, lunPrimaParte, 1, primaParte);
lunSecondaParte := length(secondaParte);
DBMS_LOB.writeappend(intermediaField, lunSecondaParte, secondaParte);
lunTerzaParte := length(terzaParte);
DBMS_LOB.writeappend(intermediaField, lunTerzaParte, terzaParte);

SELECT CAMPOCLOB INTO lob_loc FROM TABELLAESEMPIO
WHERE NUMEROCLOB = 18
FOR UPDATE;

/*cancello il contenuto del CLOB */
amount := DBMS_LOB.getlength(lob_loc);
IF amount > 0 THEN
DBMS_LOB.erase(lob_loc, amount, 1);
END IF;
writeAmount := DBMS_LOB.getlength(intermediaField);
DBMS_LOB.copy(lob_loc, intermediaField, writeAmount, 1, 1);

END;

Nessun commento: