Professional Documents
Culture Documents
• Introduction to LOBs
• Managing BFILEs by using the DBMS_LOB package
• Migrating LONG data types to LOBs
• Manipulating LOB data
• Using temporary LOBs
Single LONG column per table Multiple LOB columns per table
Up to 2 GB Up to 4 GB
LOB locator
CLOB BLOB
• Introduction to LOBs
• Managing BFILEs by using the DBMS_LOB package
• Migrating LONG data types to LOBs
• Manipulating LOB data
• Using temporary LOBs
Movie (BFILE)
User
Access
permissions
Movie (BFILE)
User
DIRECTORY
LOB_PATH =
'/oracle/lob/'
Movie (BFILE)
PROCEDURE READ (
lobsrc IN BFILE|BLOB|CLOB ,
amount IN OUT BINARY_INTEGER,
offset IN INTEGER,
buffer OUT RAW|VARCHAR2 )
PROCEDURE WRITE (
lobdst IN OUT BLOB|CLOB,
amount IN OUT BINARY_INTEGER,
offset IN INTEGER := 1,
buffer IN RAW|VARCHAR2 ) -- RAW for BLOB
• Example:
– Add a BFILE column to a table:
ALTER TABLE customers ADD video BFILE;
• Introduction to LOBs
• Managing BFILEs by using the DBMS_LOB package
• Migrating LONG data types to LOBs
• Manipulating LOB data
• Using temporary LOBs
• Introduction to LOBs
• Managing BFILEs by using the DBMS_LOB package
• Migrating LONG data types to LOBs
• Manipulating LOB data
• Using temporary LOBs
• Create a tablespace where you will put a new table with the
LOB columns.
connect /as sysdba
Initialize the column LOB locator value with the DEFAULT option
or the DML statements using:
• EMPTY_CLOB() function for a CLOB column
• EMPTY_BLOB() function for a BLOB column
connect oe
set serveroutput on
set verify on
set term on
set linesize 200
Create the
CREATE OR procedure
REPLACE to read
PROCEDURE LOBs from the table:
read_lob
IS
v_lob_loc BLOB;
CURSOR profiles_cur IS
SELECT id, full_name, resume, picture
FROM customer_profiles;
v_profiles_rec customer_profiles%ROWTYPE;
BEGIN
OPEN profiles_cur;
LOOP
FETCH profiles_cur INTO v_profiles_rec;
v_lob_loc := v_profiles_rec.picture;
DBMS_OUTPUT.PUT_LINE('The length is: '||
DBMS_LOB.GETLENGTH(v_lob_loc));
DBMS_OUTPUT.PUT_LINE('The ID is: '|| v_profiles_rec.id);
DBMS_OUTPUT.PUT_LINE('The blob is read: '||
UTL_RAW.CAST_TO_VARCHAR2(DBMS_LOB.SUBSTR(v_lob_loc,200,1)));
EXIT WHEN profiles_cur%NOTFOUND;
END LOOP;
CLOSE profiles_cur;
END;
DECLARE
v_lobloc CLOB; -- serves as the LOB locator
v_text VARCHAR2(50) := 'Resigned = 5 June 2000';
v_amount NUMBER ; -- amount to be written
v_offset INTEGER; -- where to start writing
BEGIN
SELECT resume INTO v_lobloc FROM customer_profiles
WHERE id = 164 FOR UPDATE;
v_offset := DBMS_LOB.GETLENGTH(v_lobloc) + 2;
v_amount := length(v_text);
DBMS_LOB.WRITE (v_lobloc, v_amount, v_offset, v_text);
v_text := ' Resigned = 30 September 2000';
SELECT resume INTO v_lobloc FROM customer_profiles
WHERE id = 150 FOR UPDATE;
v_amount := length(v_text);
DBMS_LOB.WRITEAPPEND(v_lobloc, v_amount, v_text);
COMMIT;
END;
• Query:
• Output in SQL*Plus:
ID FULL_NAME RESUME
---------- ----------------- ---------------------------
164 Charlotte Kazan Date of Birth: 8 Februa
ry 1951 Resigned = 5 June 2000
150 Harry Dean Fonda Date of Birth: 1 June 1
956 Resigned = 30 September 2000
• Output in SQL Developer:
DBMS_LOB.SUBSTR(RESUME,5,18) SQL*Plus
-----------------------------------------------
-----------------------------------------------
DBMS_LOB.INSTR(RESUME,'=')
--------------------------
Febru
40
June
36
SQL Developer
DECLARE
text VARCHAR2(4001);
BEGIN
SELECT resume INTO text
FROM customer_profiles
WHERE id = 150;
DBMS_OUTPUT.PUT_LINE('text is: '|| text);
END;
/
• Introduction to LOBs
• Managing BFILEs by using the DBMS_LOB package
• Migrating LONG data types to LOBs
• Manipulating LOB data
• Using temporary LOBs
• Temporary LOBs:
– Provide an interface to support creation of LOBs that act like
local variables
– Can be BLOBs, CLOBs, or NCLOBs
– Are not associated with a specific table
– Are created by using the DBMS_LOB.CREATETEMPORARY
procedure
– Use DBMS_LOB routines
• The lifetime of a temporary LOB is a session.
• Temporary LOBs are useful for transforming data in
permanent internal LOBs.