Dbms_lob.getlength
Hi.
Please, I need a help using DBMS_LOB.
I have a table with a BLOB column. When I need do a INSERT or UPDATE, a trigger is executed to give no permission to insert images with size > 1Mb.
See the trigger text below:
TRIGGER TSINMETRO.HW_TRG_DDI_AFTER
BEFORE INSERT OR UPDATE
ON tsinmetro.hw_doc_digitalizados
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
declare
vblob blob;
begin
DBMS_LOB.CREATETEMPORARY(vblob,true,dbms_lob.session);
vblob:=:new.im_arquivo;
if dbms_lob.getlength(vblob) > 1000000 then
--if dbms_lob.getlength(vblob) > 1572864 then
raise_application_error(-20003,'Tamanho da imagem superior a 1 Mb');
DBMS_LOB.FREETEMPORARY(vblob);
end if;
end;
The problem is: some times is inserted images with size > 1Mb. Some times it work ok (size < 1Mb).
Some ideia about what is happening ?
Thanks.
Hi,
In my application I use the following trigger code that works fine:
if dbms_lob.getlength(:new.my_blob_column) > xxx then
Did you try using directly the code:
if dbms_lob.getlength(:new.im_arquivo) > ...
instead of:
if dbms_lob.getlength(vblob) >...
Similar Messages
-
Dbms_lob.getlength() returns different source and destination lengths
I am fairly new to PL/SQL so maybe this is an obvious problem but here goes. I am updating a clob field with a text file ~5KB in size. The field updates fine (as far as I can tell). Before I update the field, I open the source file as a bfile and then inquire the length using dbms_lob.getlength(). I then update the clob field using dbms_lob.loadclobfromfile(). This seems to work fine. However, when I use dbms_lob.getlength() on the destination object returned by dbms_lob.loadclobfromfile(), I get a length 3 characters less than then the source object (5072 vs 5075). Both the source and destination offsets are set to 1.
Probing on what documentation I could find, I found this at http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28419/d_lob.htm#i998484:
"The length returned for a BFILE includes the EOF, if it exists. Any 0-byte or space filler in the LOB caused by previous ERASE or WRITE operations is also included in the length count. The length of an empty internal LOB is 0."
I did not create the source file and I believe it is a Unix type file (because of the lack of CRs) and I am running on Windows 7. I am also using 11g Express. Could the use of a Unix-type file for a CLOB on a Windows system be causing this character count difference?
Once I found this issue I can work around it. I just want to understand what is going on.
Thanks to all who look at this.The EOF and the LF versus CR/LF could influence the count difference, yes.
Another explain could possibly be character set conversions. The BFILE I believe counts bytes, a CLOB would count "characters" - so if the source happens to contain a few multibyte characters (UTF), then the byte count would be larger than the character count.
To help you find the cause for your exact file, then I can suggest a couple of things you might do to explore the issue:
<li>Load the file into a BLOB instead of a CLOB and see what getlength() returns for the BLOB. BLOBs would also do byte counts and not try to treat the source as text.
<li>Save the CLOB back into the filesystem and compare the original file with the exported CLOB and check the differences with some filecompare tool. -
DBMS_LOB.GETLENGTH into bytes
I am using Oralce 11.2.0.3. I am using DBMS_LOB.GETLENGTH to determine size of the CLOB . Is the value given by DBMS_LOB.GETLENGTH in bytes?
For WE8MSWIN1252 you can try directly running LENGTHB against the CLOB. LENGTHB won't work against multibyte LOBs (in which case you will have to use a function similar to the above). But if it does run then you know your CLOB is single-byte and you get the size as well.
SQL> select lengthb(c) from z_t;
LENGTHB(C)
4343
4343
4969
5414
4593
162
6 rows selected
SQL> select lengthb(nc) from z_t;
select lengthb(nc) from z_t
ORA-22998: CLOB or NCLOB in multibyte character set not supported
SQL> select clob_lengthb(nc) from z_t;
CLOB_LENGTHB(NC)
4343
4343
4969
5414
4593
162
6 rows selected -
Dbms_lob.getlength() returns different values
Hi !
I am not a developer.. So, possibly cannot answer developer specific question.
We have two instances running on 10.2.0.4 but both giving different value for
declare
xml varchar2(32676) :=
'SELECT XT_STRATEGY, ACCT_DESCRIPTION,sum(MON_PL) PL_MON ,sum(TUE_PL) PL_TUE,sum(WED_PL) PL_WED ,sum(THU_PL) PL_THU, sum(FRI_PL) PL_FRI, sum(WTD_PL) PL_WTDPL,sum(WTD_PREMIUM_PL) PL_WTDPRMPL, sum(MTD_PL) PL_MARKPL,sum(MTD_PREMIUM_PL) PL_MTDPRMPL,sum(YTD_PL) PL_YTDPL,sum(YTD_PREMIUM_PL) PL_YTDPRMPL,
DLC_REGION,DLC_ENTITY FROM (SELECT xt_strategy, acct_description ,case when blankout_date < to_char( to_date( fv[EOD] )+ (1 -to_char(to_date(to_date( fv[EOD] )),''D''))) then 0 else MON_PL end as MON_PL, case when blankout_date < to_char( to_date( fv[EOD] )+ (1 -to_char(to_date(to_date( fv[EOD] )),''D''))) then 0 else tue_pl end as tue_pl,
case when blankout_date < to_char( to_date( fv[EOD] )+ (1 -to_char(to_date(to_date( fv[EOD] )),''D''))) then 0 else WED_PL end as WED_PL, case when blankout_date < to_char( to_date( fv[EOD] )+ (1 -to_char(to_date(to_date( fv[EOD] )),''D''))) then 0 else THU_PL end as THU_PL,
case when blankout_date < to_char( to_date( fv[EOD] )+ (1 -to_char(to_date(to_date( fv[EOD] )),''D''))) then 0 else FRI_PL end as FRI_PL, case when blankout_date < to_char( to_date( fv[EOD] )+ (1 -to_char(to_date(to_date( fv[EOD] )),''D''))) then 0 else WTD_PL end as WTD_PL,
case when blankout_date < to_char( to_date( fv[EOD] )+ (1 -to_char(to_date(to_date( fv[EOD] )),''D''))) then 0 else WTD_PREMIUM_PL end as WTD_PREMIUM_PL,
case when blankout_date < (select actual_date from tbl_date where date_type = ''PLMonthStart'' and to_char(actual_date, ''MMYYYY'') = to_char(to_date( fv[EOD] ),''MMYYYY'')) then 0 else mtd_pl end as mtd_pl,
case when blankout_date < (select actual_date from tbl_date where date_type = ''PLMonthStart'' and to_char(actual_date, ''MMYYYY'') = to_char(to_date( fv[EOD] ),''MMYYYY'')) then 0 else MTD_PREMIUM_PL end as MTD_PREMIUM_PL,
case when blankout_date < (select to_date(''01-JAN-''|| to_char(to_date((fv[EOD]),''DD-MON-YYYY''),''YYYY''),''DD-MON-YYYY'') from tbl_business_date) then 0 else ytd_PL end as ytd_PL,
case when blankout_date < (select to_date(''01-JAN-''|| to_char(to_date((fv[EOD]),''DD-MON-YYYY''),''YYYY''),''DD-MON-YYYY'') from tbl_business_date) then 0 else ytd_premium_PL end as ytd_premium_PL,
IN_STRIKECURR, IN_QUOTEDCUR, case dlc_region when ''LON'' then ''LN'' when ''NYC'' then ''NY'' when ''TOK'' then ''TK'' else dlc_region
end as DLC_REGION, DLC_ENTITY,DTL_REFERENCE,TRADE_STATE,blankout_date
FROM (select a.bo_book_id as xt_strategy, a.bo_book_id as acct_description , b.mon_pl, b.tue_pl, b.wed_pl,b.thu_pl,b.fri_pl,b.wtd_pl, b.wtd_premium_pl , b.mtd_pl , b.mtd_premium_pl, b.ytd_pl, b.ytd_premium_pl,
a.BASE_CCY as IN_STRIKECURR, A.QUOTED_CCY AS IN_QUOTEDCUR, a.region_code as dlc_region, a.entity_acronym as dlc_entity, TP.EBI_TRADE_ID AS DTL_REFERENCE, a.trade_state,
case when a.trade_state in (7,8,13) then a.amendment_date when a.maturity_date > nvl(a.premium_settlement_date, to_date(''01-JAN-1900'')) then
Case when a.trade_state in (4,5 ) then trunc(a.updated_date) else a.maturity_date end else a.premium_settlement_date end as blankout_date FROM TBL_TRADE_HIST A
join vw_feed_downstream_trade_id tp on a.trade_id = tp.trade_id and tp.mapping_source_name = ''ramfx''
join tbl_pl b on a.trade_id = b.trade_id and a.version = b.trade_version and b.pl_date = (select max(pl_date) from tbl_pl d where a.trade_id = d.trade_id and pl_date <= fv[EOD] )
join tbl_bo_book bk on a.bo_book_id = bk.bo_book_id and bk.business_Area = ''OPTIONS''
where b.pl_date < fv[EOD]
AND NOT EXISTS (SELECT 1 FROM TBL_EOD_TRADES D WHERE A.TRADE_ID = D.TRADE_ID AND D.BUSINESS_DATE = fv[EOD] )
UNION
SELECT A.XT_STRATEGY, A.ACCT_DESCRIPTION, C.MON_PL,C.TUE_PL, C.WED_PL,C.THU_PL, C.FRI_PL,C.WTD_PL,C.WTD_PREMIUM_PL,C.MTD_PL,C.MTD_PREMIUM_PL,C.YTD_PL,C.YTD_PREMIUM_PL,
A.IN_STRIKECURR, A.IN_QUOTEDCUR, A.DLC_REGION,A.DLC_ENTITY, A.DTL_REFERENCE , a.trade_state , case when a.trade_state in (7,8,13) then a.DLC_BUSDATEUPD when a.IN_EXPIRATION > nvl(a.XT_SETTLEDATE, to_date(''01-JAN-1900'')) then a.IN_EXPIRATION else a.XT_SETTLEDATE end as blankout_date
FROM VW_EBI_FEED A , TBL_EOD_TRADES B , TBL_PL C
WHERE A.TRADE_ID = B.TRADE_ID AND A.VERSION = B.TRADE_VERSION AND A.TRADE_ID = C.TRADE_ID
AND A.ACCT_RISKFEED = ''D'' AND A.DLC_DEALSTATE in (''LIVE'',''PENDING'',''CANCELLED'',''REVERSED'') AND A.VERSION = C.TRADE_VERSION
AND B.BUSINESS_DATE = to_date( fv[EOD] ,''DD-MON-YYYY'') AND C.PL_DATE = to_date( fv[EOD] ,''DD-MON-YYYY'')
UNION
SELECT A.BO_BOOK_ID AS XT_STRATEGY , A.BO_BOOK_ID AS ACCT_DESCRIPTION, a.MON_PL , a.TUE_PL , a.WED_PL, a.THU_PL , a.FRI_PL, a.WTD_PL ,a.WTD_PREMIUM_PL , a.mtd_pl, a.MTD_PREMIUM_PL,a.ytd_PL , a.ytd_premium_PL,A.BASE_CCY AS IN_STRIKECURR,A.QUOTED_CCY AS IN_QUOTEDCUR,
A.REGION_CODE as dlc_region ,A.ENTITY_ACRONYM AS DLC_ENTITY, A.MAPPING_SOURCE_ID AS DTL_REFERENCE, 12 as trade_state , a.PL_DATE as blankout_date
FROM TBL_PL_ARCH A , tbl_bo_book b where a.bo_book_id = b.bo_book_id and b.business_area = ''OPTIONS''
union
select d.bo_book_id as xt_strategy, d.bo_book_id as acct_description, case when to_char(b.pl_date,''D'') = 6 then (b.inception_pl + b.inception_premium_pl ) else (b.mon_pl * -1) end as mon_pl ,
case when to_char(b.pl_date,''D'') = 2 then (b.inception_pl + b.inception_premium_pl ) when to_char(b.pl_date,''D'') > 2 then (b.tue_PL * -1) else 0 end as tue_pl, case when to_char(b.pl_date,''D'') = 3 then (b.inception_pl + b.inception_premium_pl ) when to_char(b.pl_date,''D'') > 3 then (b.wed_pl * -1) else 0 end as wed_pl ,
case when to_char(b.pl_date,''D'') = 4 then (b.inception_pl + b.inception_premium_pl) when to_char(b.pl_date,''D'') > 4 then (b.thu_pl * -1) else 0 end as thu_pl ,case when to_char(b.pl_date,''D'') = 5 then (b.inception_pl + b.inception_premium_pl) when to_char(b.pl_date,''D'') > 5 then (b.fri_pl * -1) else 0 end as fri_pl ,
(b.mon_pl + b.tue_pl + b.wed_pl + b.thu_pl + b.fri_pl) * -1 + (b.inception_pl + b.inception_premium_pl) as wtd_pl, case when b.wtd_premium_pl <> 0 then b.wtd_premium_pl *-1 + (b.inception_premium_pl) else b.wtd_premium_pl end as wtd_premium_pl , case when b.mtd_pl <> 0 then b.mtd_pl * -1 + (b.inception_pl) else b.mtd_pl end as mtd_pl ,
case when b.mtd_premium_pl <> 0 then b.mtd_premium_pl * -1 + (b.inception_premium_pl ) else b.mtd_premium_pl end as mtd_premium_pl, case when b.ytd_pl <> 0 then b.ytd_pl * -1 + (b.inception_pl) else b.ytd_pl end as ytd_pl,
case when b.ytd_premium_pl <> 0 then b.ytd_premium_pl * -1 + (b.inception_premium_pl) else b.ytd_premium_pl end as ytd_premium_pl ,
c.base_ccy as IN_STRIKECURR,c.quoted_ccy as IN_QUOTEDCUR,c.region_code as dlc_region,c.entity_acronym as dlc_entity, to_char(c.trade_id) as dtl_reference,
8 as trade_state, case when to_char(b.pl_date,''D'') = 6 then b.pl_date + 2 else b.pl_date end as blankout_date
from tbl_trade_hist a
join tbl_pl b on a.trade_id = b.trade_id and a.version = b.trade_version
join tbl_trade_hist c on a.tradE_id = c.trade_id
join tbl_pl d on c.trade_id = d.trade_id and c.version = d.trade_version and d.pl_date = (select max(e.pl_date) from tbl_pl e where c.trade_id = e.trade_id and e.pl_date <= c.amendment_date)
join tbl_bo_book f on a.bo_book_id = f.bo_book_id and f.business_area = ''OPTIONS''
where (b.bo_book_id <> d.bo_book_id or a.region_code <> c.region_code or a.entity_acronym <> c.entity_acronym or a.base_ccy <> c.base_ccy or a.quoted_ccy <> c.quoted_ccy)
and d.pl_date - b.pl_date = case when to_char(d.pl_date,''DAY'') = ''MON'' then 3 else 1 end
and d.pl_date <= to_date( fv[EOD] ,''DD-MON-YYYY'')
union all
select b.bo_book_id as xt_strategy, b.bo_book_id as acct_description,case when to_char(b.pl_date,''D'') = 6 then (b.inception_pl * -1 + b.inception_premium_pl * -1) else (b.mon_pl ) end as mon_pl ,
case when to_char(b.pl_date,''D'') = 2 then (b.inception_pl * -1 + b.inception_premium_pl * -1) when to_char(b.pl_date,''D'') > 2 then (b.tue_PL) else 0 end as tue_pl, case when to_char(b.pl_date,''D'') = 3 then (b.inception_pl * -1 + b.inception_premium_pl * -1 ) when to_char(b.pl_date,''D'') > 3 then (b.wed_pl) else 0 end as wed_pl ,
case when to_char(b.pl_date,''D'') = 4 then (b.inception_pl * -1 + b.inception_premium_pl * -1 ) when to_char(b.pl_date,''D'') > 4 then (b.thu_pl ) else 0 end as thu_pl , case when to_char(b.pl_date,''D'') = 5 then (b.inception_pl *-1 + b.inception_premium_pl * -1) when to_char(b.pl_date,''D'') > 5 then (b.fri_pl) else 0 end as fri_pl ,
(b.mon_pl + b.tue_pl + b.wed_pl + b.thu_pl + b.fri_pl) + (b.inception_pl* -1 + b.inception_premium_pl * -1) as wtd_pl, case when b.wtd_premium_pl <> 0 then b.wtd_premium_pl + (b.inception_premium_pl * -1) else b.wtd_premium_pl end as wtd_premium_pl , case when b.mtd_pl <> 0 then b.mtd_pl + (b.inception_pl * -1) else b.mtd_pl end as mtd_pl ,
case when b.mtd_premium_pl <> 0 then b.mtd_premium_pl + (b.inception_premium_pl * -1) else b.mtd_premium_pl end as mtd_premium_pl, case when b.ytd_pl <> 0 then b.ytd_pl + (b.inception_pl * -1) else b.ytd_pl end as ytd_pl,
case when b.ytd_premium_pl <> 0 then b.ytd_premium_pl + (b.inception_premium_pl * -1) else b.ytd_premium_pl end as ytd_premium_pl ,
a.base_ccy as IN_STRIKECURR, a.quoted_ccy as IN_QUOTEDCUR, a.region_code as dlc_region,a.entity_acronym as dlc_entity, to_char(a.trade_id) as dtl_reference, 8 as trade_state,
case when to_char(b.pl_date,''D'') = 6 then b.pl_date + 2 else b.pl_date end as blankout_date
from tbl_trade_hist a
join tbl_pl b on a.trade_id = b.trade_id and a.version = b.trade_version
join tbl_trade_hist c on a.tradE_id = c.trade_id join tbl_pl d on c.trade_id = d.trade_id and c.version = d.trade_version and d.pl_date = (select max(e.pl_date) from tbl_pl e where c.trade_id = e.trade_id and e.pl_date <= c.amendment_date)
join tbl_bo_book f on a.bo_book_id = f.bo_book_id and f.business_area = ''OPTIONS''
where (b.bo_book_id <> d.bo_book_id or a.region_code <> c.region_code or a.entity_acronym <> c.entity_acronym or a.base_ccy <> c.base_ccy or a.quoted_ccy <> c.quoted_ccy)
and d.pl_date - b.pl_date = case when to_char(d.pl_date,''DAY'') = ''MON'' then 3 else 1 end and d.pl_date <= to_date( fv[EOD] ,''DD-MON-YYYY'') ))
group by xt_strategy,ACCT_DESCRIPTION,DLC_REGION,DLC_ENTITY';
c clob;
s varchar2(25000);
i numeric := 424;
begin
c := xml;
dbms_output.put_line('length of clob: ' || dbms_lob.getlength(c));
dbms_output.put_line('split clob at: ' || i);
dbms_output.put_line('desired substring length: ' || to_char(dbms_lob.getlength(c) - i));
s := dbms_lob.substr(c, dbms_lob.getlength(c) - i, i + 1);
dbms_output.put_line('length of substring: ' || length(s));
end;I checked for the database characterset and they are same - AL32UTF8
The result we get is
Output from MD1 database
length of clob: 10616
split clob at: 424
desired substring length: 10192
length of substring: 10192
Output from MD2 database
length of clob: 10616
split clob at: 424
desired substring length: 10192
length of substring: 8191Any idea why there is this discrepancy ?
Edited by: USER101 on Feb 19, 2010 4:24 PMThe EOF and the LF versus CR/LF could influence the count difference, yes.
Another explain could possibly be character set conversions. The BFILE I believe counts bytes, a CLOB would count "characters" - so if the source happens to contain a few multibyte characters (UTF), then the byte count would be larger than the character count.
To help you find the cause for your exact file, then I can suggest a couple of things you might do to explore the issue:
<li>Load the file into a BLOB instead of a CLOB and see what getlength() returns for the BLOB. BLOBs would also do byte counts and not try to treat the source as text.
<li>Save the CLOB back into the filesystem and compare the original file with the exported CLOB and check the differences with some filecompare tool. -
DBMS_LOB.GETLENGTH Bytes? K? MB? GB?
What is size of a BLOB or LOB returned with DBMS_LOB.GETLENGTH? Is it bytes? K? MB?
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/d_lob.htm#sthref3763
GETLENGTH Functions
This function gets the length of the specified LOB. The length in bytes or characters is returned. -
Difference between LOB segement size and DBMS_LOB.GETLENGTH
SQL> select bytes/1024/1024 from dba_segments where segment_name='SYS_LOB0000130003C00019$$';
BYTES/1024/1024
14772
SQL> select TABLE_NAME,COLUMN_NAME from dba_lobs where SEGMENT_NAME='SYS_LOB0000130003C00019$$';
TABLE_NAME
COLUMN_NAME
TBL
C1
SQL> select sum(DBMS_LOB.GETLENGTH(C1))/1024/1024 from TBL;
SUM(DBMS_LOB.GETLENGTH(C1)
30.0376911
why is there a discrepancy between the two sizes (14GB and 30MB).
Here are the storage characteristics from the TBL ddl for the C1 LOB column:
TABLESPACE TBLSPC ENABLE STORAGE IN ROW CHUNK 8192 PCTVERSION 10
NOCACHE LOGGING
STORAGE(INITIAL 1048576 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT))user10508599 wrote:
why is there a discrepancy between the two sizes (14GB and 30MB).
Here are the storage characteristics from the TBL ddl for the C1 LOB column:
TABLESPACE TBLSPC ENABLE STORAGE IN ROW CHUNK 8192 PCTVERSION 10
NOCACHE LOGGING
STORAGE(INITIAL 1048576 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT))
According to your storage parameters it only requires 14772 rows that have lob values that are stored out of row, i.e. larger than approx. 4k. For each lob segment 1M will be allocated at least, so that might a reasonable explanation.
Not sure where I had my mind when writing this, but this is certainly wrong (and no one complaining...).
You could have a lot of deleted rows in the table, or LOBs that were larger but shrinked now, or you could hit a storage allocation bug.
What is the block size of the tablespace for the LOBs?
Regards,
Randolf
Oracle related stuff blog:
http://oracle-randolf.blogspot.com/
SQLTools++ for Oracle (Open source Oracle GUI for Windows):
http://www.sqltools-plusplus.org:7676/
http://sourceforge.net/projects/sqlt-pp/
Edited by: Randolf Geist on Jun 16, 2009 7:20 AM
Corrected plain wrong statement -
Error in using DBMS_LOB Package
Hi,
I am attaching a PDF File through a database procedure as shown below
create or replace procedure load_document(document_id in number, filename IN varchar2, doc_desc IN VARCHAR2, doc_type IN VARCHAR2) as
f_lob bfile;
b_lob blob;
--document_name varchar2(30);
--mime_type varchar2(30);
document_name oea_documents.document_name%TYPE;
mime_type oea_documents.document_mime_type%TYPE;
dot_pos number;
v_file_type VARCHAR2(10);
begin
-- Find the position of the dot ('.') located in the filename
dot_pos := instr(filename,'.');
-- Get the filename without extension and use it as image name
document_name := substr(filename,1,dot_pos-1);
-- Build the mime type . Retrieve the file extension and add it to 'image/'
v_file_type := SUBSTR( filename, dot_pos+1, Length(Filename) );
IF ( UPPER(v_file_type) IN ('JPG','JPEG','TIF','TIFF','GIF') ) THEN
mime_type := 'image/'||substr( filename , dot_pos+1 , length(filename) );
ELSIF ( UPPER(v_file_type) = 'PDF' ) THEN
mime_type := 'application/pdf';
ELSIF ( UPPER(v_file_type) = 'DOC' )THEN
mime_type := 'application/msword';
ELSIF ( UPPER( v_file_type) = 'XLS') THEN
mime_type := 'application/ms-excel';
ELSE
mime_type := 'image/'||substr( filename , dot_pos+1 , length(filename) );
END IF;
insert into oea_documents (document_id,
document_name,
document_mime_type,
document,
document_description,
document_type
values(document_id, document_name, mime_type, empty_blob(),doc_desc,doc_type) return document into b_lob;
-- /!\ Directory name has to be UpperCase !
f_lob := bfilename('FILE_LOAD',filename);
o_dset_test('5');
dbms_lob.fileopen(f_lob,dbms_lob.file_readonly);
o_dset_test('6');
dbms_lob.loadfromfile(b_lob,f_lob,dbms_lob.getlength(f_lob) );
dbms_lob.fileclose(f_lob);
commit;
end;
FILE_LOAD is a folder on the database server(unix).
I have also tried to give the full path on the unix databse server instead of FILE_LOAD directory but then it gives error ora-22285.
while executing this procedure I am getting the error ora-22288(Invalid file or directory).
But this occurs randomly
For instance i tried to attach a pdf file with the name cross.pdf ,it gave me the above error and when i renamed the file as Cross.pdf it attached successfully.
The error comes whilke executing the statement dbms_lob.fileopen(f_lob,dbms_lob.file_readonly);
Please guide on the above issue.Hi,
Welcome to the forum!
user1356624 wrote:
f_lob := bfilename('FILE_LOAD',filename);
FILE_LOAD is a folder on the database server(unix).
I have also tried to give the full path on the unix databse server instead of FILE_LOAD directory but then it gives error ora-22285.The first argument to BFILENAME is the name of a directory object, as found in the directory_name column of all_directories. That is not the same thing as a folder name, which is found in the directory_path column of all_directories. Look up [CREATE DIRECTORY|http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/statements_5007.htm#sthref6211] in the SQL Language manual for a description of Oracle's directory objects.
If you have problems with this, post the results of
SELECT *
FROM all_directories;It might also be useful to see the Oracle "CREATE DIRECTORY" command that was used.
As you probably noticed, this site compresses white space by default.
When you post code or results on this site, type these 6 characters:
(small letters only, inside curly brackets) before and after sections of formatted text, to preserve spacing.
Edited by: Frank Kulash on Aug 10, 2009 3:35 PM
Added link. -
Need help in using dbms_lob.read
I need to upload a file into an Oracle table into a Blob column. The file name along with the file contents are all in one BLOB column.
Once that is done I need to read from the file and extract the file contents and load it into a staging table.
File being uploaded is a *.CSV* file.
E.g. Of the .CSV file is: ABC.csv file and its contents will look like:
1,Hello,Nisha
2,Hi,Ravi
3,Bye, Rahul
Etc…..
Therefore the table containing the BLOB column will contain:
File Creation_date
ABC.csv 09/11/2009
How can I read a file from the BLOB column and upload into a staging table?
Final Staging table should look like:
Record Number Greet Name
1 Hello Nisha
2 Hi Ravi
3 Bye Rahul
I think I am suppose to use dbms_lob.read, but I am not really sure how to use it. If there is any script, kindly mail me the same.
Thanks....Nisha,
Check this example (test) and see if it can be any help. I have utl_file and sqlldr
First Method -- I loaded alert.log successfully and you can imagine how big this file can be (5MB in my test case)
create table t1clob
( clob_text clob);
CREATE OR REPLACE DIRECTORY DIR AS '/path_to_csv_file/;
DECLARE
clob_data CLOB;
clob_file BFILE;
BEGIN
INSERT INTO t1clob
VALUES (EMPTY_CLOB ())
RETURNING clob_text INTO clob_data;
clob_file := BFILENAME ('DIR', ABC.csv');
DBMS_LOB.fileopen (clob_file);
DBMS_LOB.loadfromfile (clob_data,
clob_file,
DBMS_LOB.getlength (clob_file)
DBMS_LOB.fileclose (clob_file);
COMMIT;
END;Second Method: Use of Sqlldr
Example of controlfile
LOAD DATA
INFILE alert.log "STR '|\n'"
REPLACE INTO table t1clob
clob_text char(30000000)
)Hope this helps. -
How to insert 10 files from a directory to database,can i use dbms_lob??
Hii
I want to load 10files in my local drive into a table...how to do this.I'm able to do this individually using dbms_lob.loadfromfile and bfil but ,I want to copy all the files i that drive at time to my table...Is there any way to do this..?Okay... Here is some sample code that will help you store the files in binary format into db tables.
create table t_blob(bid integer, blbdata blob);
select * from t_blob;
create or replace directory ext_tab_dir as 'C:\Oracle\ExtTab_Dir';
grant read, write on directory ext_tab_dir to priya;
declare
src_file bfile;
dest_file blob;
len_file pls_integer;
begin
src_file:=bfilename('EXT_TAB_DIR','XML.doc');
insert into t_blob values(1,empty_blob()) returning blbdata into dest_file;
select blbdata into dest_file
from t_blob
where bid=1 for update;
dbms_lob.fileopen(src_file,dbms_lob.file_readonly);
len_file:=dbms_lob.getlength(src_file);
dbms_output.put_line(len_file);
dbms_lob.loadfromfile(dest_file,src_file,len_file);
update t_blob
set blbdata = dest_file
where bid = 1;
dbms_lob.fileclose(src_file);
end;
/I haven't used the wrap utility yet, so not much hands on with it. I guess the wrap utility at the OS level. Not sure if it will work at SQL prompt. -
PDF file created from Oracle Report is created wrongly using dbms_lob
Hi,
Please help. It is very urgent.
I am using Oracle Developer 10gR2, Oracle Report 10.1.2 on Windows 2000.
I would like to attach the PDF file created by Oracle Report to the Notification sent from Workflow.
I use the following package procedure in my Workflow.
procedure Create_File_Attachment (document_id in varchar2, display_type in varchar2,
document in out blob, document_type in out varchar2)
is
l_itemtype varchar2(100);
l_itemkey varchar2(100);
l_output_directory varchar2(30);
l_filename varchar2(255);
src_loc bfile;
bdoc blob;
src_offset number := 1;
dst_offset number := 1;
amount number;
begin
l_itemtype := substr(document_id, 1, instr(document_id, ':') - 1);
l_itemkey := substr(document_id, instr(document_id, ':') + 1, length(document_id) - 2);
l_output_directory := 'USR_TMP_DIR';
l_filename := Wf_Engine.GetItemAttrText(l_itemtype, l_itemkey, 'ATR_FILENAME');
src_loc := bfilename(l_output_directory,l_filename);
dbms_lob.createTemporary(bdoc, FALSE, dbms_lob.call);
dbms_lob.fileopen(src_loc, dbms_lob.file_readonly);
dbms_lob.loadblobfromfile(bdoc,src_loc,dbms_lob.lobmaxsize,src_offset,dst_offset);
dbms_lob.fileclose(src_loc);
amount := dbms_lob.getLength(bdoc);
dbms_lob.copy(document,bdoc,amount,1,1);
document_type := 'application/pdf; name=attach.pdf';
end Create_File_Attachment;Oracle Report created the PDF file correctly (original.pdf).
I tried to attach the PDF into my Workflow.
I can see the file is attached (attach.pdf), but it cannot be opened.
The Adobe shows : 'A drawing error occured.' each time I open the attachment.
I compare the original.pdf and the attach.pdf
However, there is one specific difference
- in original.pdf, the line started with '.' (single period)
in attach.pdf, the line is started with '..' (two periods)
Thus the attachment cannot be opened.
After I delete the period, it can be opened.
Question:
- Has someone ever succeed in attaching the PDF created from Report?
- Are the codes above is wrong?
- Or perhaps the DBMS_LOB.LOADBLOBFROMFILE always double the single period on front of the line?
- Is there any solution on this?
Note:
I have succeed in attaching PDF files unless the PDF created by the Report.
Sorry for the long post.
Please help. It is very urgent.
Any help is appreciated.
Many thanks,
BuntoroHi,
Please help. It is very urgent.
I am using Oracle Developer 10gR2, Oracle Report 10.1.2 on Windows 2000.
I would like to attach the PDF file created by Oracle Report to the Notification sent from Workflow.
I use the following package procedure in my Workflow.
procedure Create_File_Attachment (document_id in varchar2, display_type in varchar2,
document in out blob, document_type in out varchar2)
is
l_itemtype varchar2(100);
l_itemkey varchar2(100);
l_output_directory varchar2(30);
l_filename varchar2(255);
src_loc bfile;
bdoc blob;
src_offset number := 1;
dst_offset number := 1;
amount number;
begin
l_itemtype := substr(document_id, 1, instr(document_id, ':') - 1);
l_itemkey := substr(document_id, instr(document_id, ':') + 1, length(document_id) - 2);
l_output_directory := 'USR_TMP_DIR';
l_filename := Wf_Engine.GetItemAttrText(l_itemtype, l_itemkey, 'ATR_FILENAME');
src_loc := bfilename(l_output_directory,l_filename);
dbms_lob.createTemporary(bdoc, FALSE, dbms_lob.call);
dbms_lob.fileopen(src_loc, dbms_lob.file_readonly);
dbms_lob.loadblobfromfile(bdoc,src_loc,dbms_lob.lobmaxsize,src_offset,dst_offset);
dbms_lob.fileclose(src_loc);
amount := dbms_lob.getLength(bdoc);
dbms_lob.copy(document,bdoc,amount,1,1);
document_type := 'application/pdf; name=attach.pdf';
end Create_File_Attachment;Oracle Report created the PDF file correctly (original.pdf).
I tried to attach the PDF into my Workflow.
I can see the file is attached (attach.pdf), but it cannot be opened.
The Adobe shows : 'A drawing error occured.' each time I open the attachment.
I compare the original.pdf and the attach.pdf
However, there is one specific difference
- in original.pdf, the line started with '.' (single period)
in attach.pdf, the line is started with '..' (two periods)
Thus the attachment cannot be opened.
After I delete the period, it can be opened.
Question:
- Has someone ever succeed in attaching the PDF created from Report?
- Are the codes above is wrong?
- Or perhaps the DBMS_LOB.LOADBLOBFROMFILE always double the single period on front of the line?
- Is there any solution on this?
Note:
I have succeed in attaching PDF files unless the PDF created by the Report.
Sorry for the long post.
Please help. It is very urgent.
Any help is appreciated.
Many thanks,
Buntoro -
Using dbms_lob to load image into table
I am trying to load a set of images from my DB drive into a table. This works fine when I try to load only 1 record. If I try to load more than 1 record, first gets created but I get this error, and it doesn't load the images for the rest of them.
ORA-22297: warning: Open LOBs exist at transaction commit time
Cause: An attempt was made to commit a transaction with open LOBs at transaction commit time.
Action: This is just a warning. The transaction was commited successfully, but any domain or functional indexes on the open LOBs were not updated. You may want to rebuild those indexes.
Am I missing something in the code that's needed?
in_file UTL_FILE.FILE_TYPE;
bf bfile;
b blob;
src_offset integer := 1;
dest_offset integer := 1;
CURSOR get_pics is select id from emp;
BEGIN
FOR x in get_pics LOOP
BEGIN
insert into stu_pic(id,student_picture)
values(x.id,empty_blob()) returning student_picture into b;
l_picture_uploaded := 'Y';
bf := bfilename('INTERFACES',x.student_id || '.' || p_image_type);
dbms_lob.fileopen(bf,dbms_lob.file_readonly);
dbms_lob.open(b,dbms_lob.lob_readwrite);
dbms_lob.loadBlobFromFile(b,bf,dbms_lob.lobmaxsize,dest_offset,src_offset);
dbms_lob.close(b);
dbms_lob.fileclose(bf);
EXCEPTION when dup_val_on_index then null;
END;
END LOOP;
END;There are two methods you can use.
1. Create an external table with those images(BLOB column) and then use that external table to insert into another table.
Demo as follows:
This is my pdf files
C:\Saubhik\Assembly\Books\Algorithm>dir *.pdf
Volume in drive C has no label.
Volume Serial Number is 6806-ABBD
Directory of C:\Saubhik\Assembly\Books\Algorithm
08/16/2009 02:11 PM 1,208,247 algorithms.pdf
08/17/2009 01:05 PM 13,119,033 fci4all.com.Introduction_to_the
d_Analysis_of_Algorithms.pdf
09/04/2009 06:58 PM 30,375,002 sedgewick-algorithms.pdf
3 File(s) 44,702,282 bytes
0 Dir(s) 7,474,593,792 bytes free
C:\Saubhik\Assembly\Books\Algorithm>This is my file with which I'll load the pdf files as BLOB
C:\Saubhik\Assembly\Books\Algorithm>type mypdfs.txt
Algorithms.pdf,algorithms.pdf
Sedgewick-Algorithms.pdf,sedgewick-algorithms.pdf
C:\Saubhik\Assembly\Books\Algorithm>Now the actual code
SQL> /* This is my directory object */
SQL> CREATE or REPLACE DIRECTORY saubhik AS 'C:\Saubhik\Assembly\Books\Algorithm';
Directory created.
SQL> /* Now my external table */
SQL> /* This table contains two columns. 1.pdfname contains the name of the file
DOC> and 2.pdfFile is a BLOB column contains the actual pdf*/
SQL> CREATE TABLE mypdf_external (pdfname VARCHAR2(50),pdfFile BLOB)
2 ORGANIZATION EXTERNAL (
3 TYPE ORACLE_LOADER
4 DEFAULT DIRECTORY saubhik
5 ACCESS PARAMETERS (
6 RECORDS DELIMITED BY NEWLINE
7 BADFILE saubhik:'lob_tab_%a_%p.bad'
8 LOGFILE saubhik:'lob_tab_%a_%p.log'
9 FIELDS TERMINATED BY ','
10 MISSING FIELD VALUES ARE NULL
11 (pdfname char(100),blob_file_name CHAR(100))
12 COLUMN TRANSFORMS (pdfFile FROM lobfile(blob_file_name) FROM (saubhik) BLOB)
13 )
14 LOCATION('mypdfs.txt')
15 )
16 REJECT LIMIT UNLIMITED;
Table created.
SQL> SELECT pdfname,DBMS_LOB.getlength(pdfFile) pdfFileLength
2 FROM mypdf_external;
PDFNAME PDFFILELENGTH
Algorithms.pdf 1208247
Sedgewick-Algorithms.pdf 30375002
SQL> Now, you can use this table for any operation very easily. Even for your loading into another table!.
2. Use of DBMS_LOB like this
/* Loading a image Winter.jpg in the BLOB column as BLOB!*/
DECLARE
v_src_blob_locator BFILE := BFILENAME('SAUBHIK', 'Winter.jpg');
v_amount_to_load INTEGER := 4000;
dest_lob_loc BLOB;
BEGIN
--Insert a empty row with id 1
INSERT INTO test_my_blob_clob VALUES(1,EMPTY_BLOB(),EMPTY_CLOB())
RETURNING BLOB_COL INTO dest_lob_loc;
DBMS_LOB.open(v_src_blob_locator, DBMS_LOB.lob_readonly);
v_amount_to_load := DBMS_LOB.getlength(v_src_blob_locator);
DBMS_LOB.loadfromfile(dest_lob_loc, v_src_blob_locator, v_amount_to_load);
DBMS_LOB.close(v_src_blob_locator);
COMMIT;
--id=1 is created with Winter.jpg populated in BLOB_COL and CLOB_COL is empty.
END;Now user this code to create a procedure with parameter and use that in loop. -
i am using this method dbms_lob.getlength(column name) from table name;
as far as i know this column can be a clob column or blob column or bfile column
i have a row in these columns inserted via java at the sql prompt i just want to see the length of the row of each of these cols
when i say
dbms_lob.getlength(column name) from table name in case of a bfile or a clob column it works perfectly and shows me the length for the row
but with blob i get the following error
non existing directory or file for getlength
nullNo insert. You have to insert it by writing the part before then insert, writing the new portion, and then write the part after then place you inserted.
-
Dbms_lob.FileOpen failed using remote directory in oracle 10g
when i create a directory with the remote shared path like '\\kf-kjyxp\sharepath\temporary' . then in a pl/sql block i use dbms_lob.fileopen to open bfile object , a failure occurs .but the situation does not happen when i use a local diretory like 'd:\sharepath\temporary'.the most puzzling thing is that when i use dbms_lob.FILEISOPEN to check whether i can access the file ,it returns 1 which means succeed . And dbms_lob.GETLENGTH() also returns the right value.
here is the testing code:
declare
pFile bfile;
sDir varchar2(255);
sFileName varchar2(255);
bExist integer := 0;
Amount Integer := 4000;
begin
sDir := 'SRC_PATH';
sFileName := 'ele_onlinedoc.ico';
pFile := bfilename(sDir, sFileName);
--checking file existence
bExist := DBMS_LOB.FILEEXISTS(pFile);
if bExist = 1 then
dbms_output.put_line('File Exist');
else
dbms_output.put_line('File Not Exist');
end if;
--check file size
Amount := dbms_lob.Getlength(pFile);
dbms_output.put_line(TO_CHAR(Amount));
if dbms_lob.FILEISOPEN(pFile) = 1 then
dbms_output.put_line('File is already Opend');
else
dbms_output.put_line('File is closed');
end if;
dbms_lob.fileOpen(pFile,dbms_lob.file_readonly);
dbms_lob.fileclose(pFile);
EXCEPTION
When VALUE_ERROR then
dbms_output.put_line('Exception Value_error');
When OTHERS then
dbms_output.put_line('Exception OTHERS');
dbms_output.put_line(to_char(SQLCODE));
dbms_output.put_line(sQLERRM);
end;
OUTPUT:
File Exist
1078
File is closed
Exception OTHERS
-22288
ORA-22288: file or LOB operation FILEOPEN failed
we can see that file is detected and also we can get the right size(1078 byte) ,
but fail to open the file.
so what`s the problem .
thanks!ORA-22288: file or LOB operation string failed string
Cause: The operation attempted on the file or LOB failed.
Action: See the next error message in the error stack for more detailed information. Also, verify that the file or LOB exists and that the necessary privileges are set for the specified operation. If the error still persists, report the error to the DBA. -
Hi,
I am trying to load an xml file into my xmltable by using one user defined function called "getClobDocument".
Here is the function source code.
create or replace function getClobDocument(
filename in varchar2,
charset in varchar2 default NULL)
return CLOB deterministic
is
file bfile := bfilename('XMLDIR',filename);
charContent CLOB := ' ';
targetFile bfile;
lang_ctx number := DBMS_LOB.default_lang_ctx;
charset_id number := 0;
src_offset number := 1 ;
dst_offset number := 1 ;
warning number;
begin
dbms_output.put_line('file_name '||filename);
if charset is not null then
charset_id := NLS_CHARSET_ID(charset);
end if;
targetFile := file;
dbms_output.put_line( DBMS_LOB.getLength(targetFile));
DBMS_LOB.fileopen(targetFile, DBMS_LOB.file_readonly);
DBMS_LOB.LOADCLOBFROMFILE(charContent, targetFile,
DBMS_LOB.getLength(targetFile), src_offset, dst_offset,
charset_id, lang_ctx,warning);
DBMS_LOB.fileclose(targetFile);
dbms_output.put_line('char_content '||charContent);
return charContent;
end;
I inserted an xml file called po.xml into the xml tabel .But i selected the data from xml table only one line is isplayed.
The rest of the xml file is not inserted into the xml table.
I am also getting the following error.
"PL/SQL: numeric or value error: host bind array too small"
can anybody tell the reason for it.
cheers
RRKJust for the sake of clarification ... What value are you specifying for p_dir variable?
It should be a directory object created in the database.
It can be created with a command like
CREATE OR REPLACE DIRECTORY BFILE_DIR AS 'e:\middle-east\ssm';
and the user who is going to call the function should have read access on this directory object which can be granted with a command like
grant read on directory BFILE_DIR to FUNCTIONCALLINGUSER;
I hope this helps with what you are trying
Best Regards -
Dbms_lob.loadfromfile help
sorry in advance if this is basic - SQL developer virgin here . . .
Using the examples in this document
http://download-west.oracle.com/docs/cd/B14117_01/appdev.101/b10840/mm_uses.htm
specifically the code:
INSERT INTO soundtable(id, sound) VALUES (1, EMPTY_BLOB());
COMMIT;
DECLARE
f_lob BFILE := BFILENAME('AUDDIR','chimes.wav');
b_lob BLOB;
Lob BLOB;
Length INTEGER;
BEGIN
SELECT sound INTO b_lob FROM soundtable WHERE id=1 FOR UPDATE;
-- Open the LOBs.
dbms_lob.open(f_lob, dbms_lob.file_readonly);
dbms_lob.open(b_lob, dbms_lob.lob_readwrite);
dbms_lob.loadfromfile
(b_lob, f_lob, dbms_lob.getlength(f_lob));
-- Close the LOBs.
dbms_lob.close(b_lob);
dbms_lob.close(f_lob);
COMMIT;
-- Select the LOB:
SELECT sound INTO Lob FROM soundtable
WHERE ID = 1;
-- Opening the LOB is optional.
DBMS_LOB.OPEN (Lob, DBMS_LOB.LOB_READONLY);
-- Get the length of the LOB.
length := DBMS_LOB.GETLENGTH(Lob);
IF length IS NULL THEN
DBMS_OUTPUT.PUT_LINE('LOB is null.');
ELSE
DBMS_OUTPUT.PUT_LINE('The length is '|| length);
END IF;
-- Closing the LOB is mandatory if you have opened it.
DBMS_LOB.CLOSE (Lob);
END;
2 questions for anyone who has done this before or has an opinion:
1) In the initial insert -- INSERT INTO soundtable(id, sound) VALUES (1, EMPTY_BLOB()); -- if the value for the column "id" is sequence.next_val I could end up with an "id" of 3012 for example:
so how would I find out what value the "id" is before doing the next step:
SELECT sound INTO b_lob FROM soundtable WHERE id=1 FOR UPDATE;
I would have to do:
SELECT sound INTO b_lob FROM soundtable WHERE id=<whatever_was_generated_by_the_sequence> FOR UPDATE;
and if there are 10 people inserting at about the same time, how the heck am I supposed to know which "id" goes with which content file?
any help is appreciatedYou can use the returning clause to store the generated sequence value that was inserted into id into a variable, then reference that variable in the rest of your code, as demonstrated below.
SCOTT@10gXE> CREATE TABLE soundtable
2 (id NUMBER,
3 sound BLOB DEFAULT EMPTY_BLOB ())
4 /
Table created.
SCOTT@10gXE> CREATE SEQUENCE your_sequence
2 /
Sequence created.
SCOTT@10gXE> VARIABLE g_id_seq NUMBER
SCOTT@10gXE> INSERT INTO soundtable (id)
2 VALUES (your_sequence.NEXTVAL)
3 RETURNING id INTO :g_id_seq
4 /
1 row created.
SCOTT@10gXE> COMMIT
2 /
Commit complete.
SCOTT@10gXE> CREATE OR REPLACE DIRECTORY auddir AS 'C:\WINDOWS\Media'
2 /
Directory created.
SCOTT@10gXE> SET SERVEROUTPUT ON
SCOTT@10gXE> DECLARE
2 f_lob BFILE := BFILENAME ('AUDDIR', 'chimes.wav');
3 b_lob BLOB;
4 Lob BLOB;
5 Length INTEGER;
6 BEGIN
7
8 SELECT sound
9 INTO b_lob
10 FROM soundtable
11 WHERE id = :g_id_seq
12 FOR UPDATE;
13
14 dbms_lob.open (f_lob, dbms_lob.file_readonly);
15 dbms_lob.open (b_lob, dbms_lob.lob_readwrite);
16 dbms_lob.loadfromfile
17 (b_lob, f_lob, dbms_lob.getlength (f_lob));
18 dbms_lob.close(b_lob);
19 dbms_lob.close(f_lob);
20 COMMIT;
21
22 SELECT sound
23 INTO Lob
24 FROM soundtable
25 WHERE ID = :g_id_seq;
26 length := DBMS_LOB.GETLENGTH (Lob);
27 IF length IS NULL THEN
28 DBMS_OUTPUT.PUT_LINE ('LOB is null.');
29 ELSE
30 DBMS_OUTPUT.PUT_LINE ('The length is '|| length);
31 END IF;
32 END;
33 /
The length is 55776
PL/SQL procedure successfully completed.
SCOTT@10gXE>
Maybe you are looking for
-
Airtunes no longer working with AirPort Express? can anyone help?!
so i recently bought a refurbished airport express and i loved how i could stream audio over the wireless network via airtunes but the delay was a bit annoying so i took it upon myself to try and find a solution to this. I bought a reverse audio spli
-
How to Change Fiscal year in Consolidation
HI all, How to change Fiscal year in consolidation...iam posting the documents.... not showing any valuues in consolidation report only shwoing documents nos only ... bcoz its whoing 2007 fiscal year.... regards JK
-
Preview and print a printout in production order release
Iu2019m using CO02 to release and save the production order. Currently u201Cbatch cardu201D printout is automatically printed in production order release. I want to change this to preview the printout before printout is send to the printer. Can someo
-
Hi , I have following situation on our PO. We create a PO with line info as ITEM DATE PRICE QTY ABC 1-JUN-08 100 1000 we provide this to supplier and after their assesment after a week or so supplier gives back tentive ship date 1-JUN-08 300 3-JUN-08
-
I have upgraded to iOS 5. Now I have lost the ability to print.
How can I fix this? My printer isn't listed, even though my printer & iPad are using the same wireless network (wi-Fi)