Saturday, October 3, 2009

Reading and printing attachments in Oracle apps Text files and HTML files only

DECLARE
l_blob BLOB;
l_blob_length NUMBER;
l_amount BINARY_INTEGER := 10000;
l_offset INTEGER := 1;
l_buffer RAW(20000);
l_text_buffer VARCHAR2(32767);
l_break_at PLS_INTEGER := 250;
BEGIN
-- Get the BLOB from the APPLSYS.FND_LOBS table.
/* SELECT file_data
INTO l_blob
FROM fnd_lobs
WHERE file_id= 331001; --221148;
*/
select file_data into l_blob
from fnd_lobs
where file_id in (
SELECT media_id
FROM FND_ATTACHED_DOCS_FORM_VL
where function_name='CIM_PA_PAXPREPR_PROJECT'
and attached_document_id in (
SELECT attached_document_id
FROM FND_ATTACHED_DOCUMENTS
where pk1_value=5465 -- project id
and entity_name='PA_PROJECTS'
)
) and file_name='Mail from Chanakya K.txt';
-- Figure out how long the BLOB is.
l_blob_length := DBMS_LOB.GETLENGTH(l_blob);
-- loop through the BLOB as many times as necessary to
-- get all its data.
FOR i IN 1..CEIL(l_blob_length/l_amount) LOOP
-- Read in the given chunk of the BLOB.
DBMS_LOB.READ(l_blob,
l_amount,
l_offset,
l_buffer
);
-- The DBMS_LOB.READ procedure dictates that its output be RAW.
-- This next procedure converts that RAW data to character data.
l_text_buffer := UTL_RAW.CAST_TO_VARCHAR2(l_buffer);
-- DBMS_OUTPUT.PUT_LINE('==================================');
-- DBMS_OUTPUT.PUT_LINE('Text Buffer: 'length(l_text_buffer));
-- DBMS_OUTPUT.PUT_LINE('==================================');
-- Now that we have character data (up to l_amount characters long),
-- chunk it out so that we can do whatever we need to with it.
FOR j IN 1..CEIL(LENGTH(l_text_buffer) / l_break_at) LOOP
DBMS_OUTPUT.PUT_LINE(SUBSTR(l_text_buffer
,(((j - 1) * l_break_at) + 1)
,LEAST(LENGTH(l_text_buffer)-((j - 1) * l_break_at),l_break_at
)
)
);
END LOOP;
-- For the next iteration through the BLOB, bump up your offset
-- location, where you start reading from
l_offset := l_offset + l_amount;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('ERROR: ' SUBSTR(SQLERRM,1,247));
RAISE;
END;
/

No comments:

Post a Comment

Blog Archive