DBA Tips Archive for Oracle |
|
Temporary LOBs
by Jeff Hunter, Sr. Database Administrator
Contents
Overview
Temporary LOBs are used just like any other local variable and are used when
you need to perform some operations on LOB data without having to store it
in the database.
Operations
Performance
Where are Temporary LOBs Stored?
Creating a Temporary LOB
To create a temporary LOB, use the
Freeing a Temporary LOB
NOTE: If you were to subsequently assign this lob locator
to another lob locator, the latter one is also freed and
marked invalid.
Check if a LOB is Temporary
Temporary LOB Example
Copyright (c) 1998-2012 Jeffrey M. Hunter. All rights reserved.
All articles, scripts and material located at the Internet address of http://www.idevelopment.info is the copyright of Jeffrey M. Hunter
and is protected under copyright laws of the United States. This document may not be hosted on any other site without my express,
prior, written permission. Application to host any of the material elsewhere can be made by contacting me at jhunter@idevelopment.info.
I have made every effort and taken great care in making sure that the material included on my web site is technically accurate,
but I disclaim any and all responsibility for any loss, damage or destruction of data or any other property which may arise from
relying on it. I will in no case be liable for any monetary damages arising from such loss, damage or destruction.
Temporary LOBs, as the name would suggest, are not permanent and hence not
stored in the database. Normally, the temporary LOB would exist
throughout the entire duration of a user's session. However, it is possible
to specify that you want the temporary LOB to exist only during the duration
of the current program call at the time of its creation.
The operations you can perform on temporary LOBs are create, access, update,
and then freeing the temporary LOB. All of the procedures and functions for
workings with temporary LOBs are located within the
DBMS_LOB PL/SQL package.
For performance reasons, there is no logging or redo information
generated for temporary LOBs.
Temporary LOBs are created in your temporary tablespace.
When creating a temporary LOB, it is already set to empty. There is
no need, and in fact, you cannot use the
EMPTY_BLOB() or
EMPTY_CLOB() functions with temporary LOBs.
DBMS_LOB.CREATETEMPORARY()
procedure:
DBMS_LOB.CREATETEMPORARY(
lob_loc IN OUT NOCOPY {BLOB | CLOB CHARACTER SET ANY_CS}
, cache IN BOOLEAN
, duration IN PLS_INTEGER := DBMS_LOB.SESSION
)
Looking at the above procedure, you can see that that it will create
a temporary LOB with the locator being returned in the lob_loc
variable. Also created with the temporary LOB is a temporary LOB index
in the default temporary tablespace. The duration parameter is used to
specify the lifetime of the temporary LOB and defaults to the session
(DBMS_LOB.SESSION). It is also possible to set the duration
parameter to the current program by using the integer DBMS_LOB.CALL.
DBMS_LOB.FREETEMPORARY(
lob_loc IN OUT NOCOPY {BLOB | CLOB CHARACTER SET ANY_CS}
)
Use the freetemporary() procedure to free a created
CLOB or BLOB in your temporary tablespace. After calling this
procedure, the lob locator associated with the temporary LOB
is marked invalid.
DBMS_LOB.ISTEMPORARY(
lob_loc IN {BLOB | CLOB CHARACTER SET ANY_CS}
)
Use the istemporary() function to determine if
a given lob locator points to a temporary LOB or a
persistent LOB. This function returns an integer
value of 1 for a temporary LOB and 0 for a
persistent LOB.
CREATE OR REPLACE PROCEDURE testTemporaryLob
AS
temporaryLob CLOB;
amount NUMBER;
offset NUMBER := 1;
buffer VARCHAR2(100) := 'Testing a temporary LOB. ';
buffer2 VARCHAR2(100) := 'Appended data.';
BEGIN
DBMS_OUTPUT.ENABLE(1000000);
-- --------------------
-- CREATE TEMPORARY LOB
-- --------------------
DBMS_LOB.CREATETEMPORARY(
lob_loc => temporaryLob
, cache => true
, dur => dbms_lob.session
);
-- -------------------------------
-- TEST TO SEE IF LOB IS TEMPORARY
-- -------------------------------
IF (DBMS_LOB.ISTEMPORARY(temporaryLob) = 1) THEN
DBMS_OUTPUT.PUT_LINE('This is a temporary LOB.');
ELSE
DBMS_OUTPUT.PUT_LINE('This is a persistent LOB.');
END IF;
-- -----------------------------------
-- OPEN TEMPORARY LOB FOR READ / WRITE
-- -----------------------------------
DBMS_LOB.OPEN(
lob_loc => temporaryLob
, open_mode => DBMS_LOB.LOB_READWRITE
);
-- -----------------------------------
-- WRITE SMALL BUFFER TO TEMPORARY LOB
-- -----------------------------------
DBMS_LOB.WRITE(
lob_loc => temporaryLob
, amount => LENGTH(buffer)
, offset => offset
, buffer => buffer
);
-- --------------------------------------
-- APPEND A SMALL BUFFER TO TEMPORARY LOB
-- --------------------------------------
DBMS_LOB.WRITEAPPEND(
lob_loc => temporaryLob
, amount => LENGTH(buffer2)
, buffer => buffer2
);
-- -----------------------------------
-- GET CURRENT LENGTH OF TEMPORARY LOB
-- -----------------------------------
DBMS_OUTPUT.PUT_LINE(
'Current length of temporary LOB: ' || DBMS_LOB.GETLENGTH(temporaryLob)
);
-- -----------------------------------
-- PRINT CONTENTS OF THE TEMPORARY LOB
-- -----------------------------------
DBMS_OUTPUT.PUT_LINE('Current content of temporary LOB:');
DBMS_OUTPUT.PUT_LINE(DBMS_LOB.SUBSTR(temporaryLob, DBMS_LOB.GETLENGTH(temporaryLob), 1));
-- --------------------------
-- CLOSE / FREE TEMPORARY LOB
-- --------------------------
DBMS_LOB.CLOSE(lob_loc => temporaryLob);
DBMS_LOB.FREETEMPORARY(lob_loc => temporaryLob);
END;
/
Procedure created.
SQL> exec testTemporaryLob
This is a temporary LOB.
Current length of temporary LOB: 39
Current content of temporary LOB:
Testing a temporary LOB. Appended data.
PL/SQL procedure successfully completed.
Sunday, 23-Feb-2003 00:00:00 EST
Page Count: 35241