您的位置:首页 > 数据库

How To Load CLOB Data from a File into a CLOB column using PL/SQL

2016-12-29 10:14 656 查看
主题:How To Load CLOB Data from a File into a CLOB column using PL/SQL
 文档 ID:437432.1类型:HOWTO
 Modified Date:02-OCT-2007状态:MODERATED
In this Document
  Goal
  Solution[align=center][/align][align=center]
This document is being delivered to you via Oracle Support's Rapid Visibility (RaV) process, and therefore has not been subject to an independent technical review.
[/align]Applies to:PL/SQL - Version: 10.2.0.3
Information in this document applies to any platform.GoalThe following code demonstates how to load CLOB data (greater than 32 KBytes) from a file into a CLOB column. The package DBMS_LOB is used to access a file stored on the operating system and load that data into a CLOB column. The DBMS_LOB.LOADCLOBFROMFILE() procedure loads the contents of the BFILE into the CLOB column using DBMS_LOB.LOBMAXSIZE which loads until the end of the BFILE is reached. This method allows the data to be loaded up to the maximum size that the CLOB column can hold. Passing DBMS_LOB.LOBMAXSIZE causes the procedure to read the entire BFILE. This is a useful technique for reading the entire LOB without introspecting the size of the LOB.Solution CREATE OR REPLACE DIRECTORY workdir as '/tmp/laura'; --  Change the directoryDROP TABLE test_clob;CREATE TABLE test_clob (id NUMBER, col_clob CLOB);INSERT INTO test_clob VALUES(1, EMPTY_CLOB());CREATE OR REPLACE PROCEDURE file_to_clob IS 
  b_fil bfile := BFILENAME('WORKDIR', 'f.txt'); -- Ensure f.txtexists 
  v_clob CLOB; 
  dest_offset NUMBER := 1; 
  source_offset NUMBER := 1; 
  src_csid NUMBER := NLS_CHARSET_ID('UTF8');
  lang_ctx INTEGER := DBMS_LOB.DEFAULT_LANG_CTX; 
  warn INTEGER; 
BEGIN 
  -- Retrieve the lob locator 
  SELECT col_clobINTO v_clob FROM test_clob WHERE id = 1 FOR UPDATE; 
  
  -- Open the target CLOB and the source BFILE 
  DBMS_LOB.OPEN(v_clob, DBMS_LOB.LOB_READWRITE); 
  DBMS_LOB.OPEN(b_fil, DBMS_LOB.FILE_READONLY);  -- Load the contents of the bfile into the CLOB column 
  DBMS_LOB.LOADCLOBFROMFILE(v_clob, b_fil, 
                            DBMS_LOB.LOBMAXSIZE, 
                            dest_offset, 
                            source_offset, 
                            src_csid, 
                            lang_ctx, warn);  -- Check for the warning 
  IF warn = DBMS_LOB.WARN_INCONVERTIBLE_CHAR THEN 
    DBMS_OUTPUT.PUT_LINE('SomeCharacters couldn''t be converted'); 
  END IF; 
  
  -- Close the LOBs 
  DBMS_LOB.CLOSE(v_clob); 
  DBMS_LOB.CLOSE(b_fil);END; 
/
To test:

SQL> EXEC file_to_clob;

PL/SQL procedure successfully completed.

SQL> SELECT DBMS_LOB.GETLENGTH(col_clob) FROM test_clob;

DBMS_LOB.GETLENGTH(COL_CLOB)
----------------------------
121094
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: