如何在oracle sql developer中导出clob字段数据。目前clob字段数据无法在oracle sql developer中导出。
答案 0 :(得分:11)
如果您不想(或不能)export and import您的数据,并且真的希望它作为一组插入语句,您可以使用SQL Developer的内置格式化工具来自动拆分您的CLOB多个小块,足以作为字符串文字有效,然后将结果假脱机到文件:
spool clob_export.sql
select /*insert*/ * from your_table;
spool off
使用更新的版本,您可以使用the sqlformat
command来控制输出格式,而无需修改查询;这相当于:
set sqlformat insert
spool clob_export.sql
select * from your_table;
spool off
生成的insert语句类似于:
REM INSERTING into YOUR_TABLE
SET DEFINE OFF;
Insert into YOUR_TABLE (ID,CLOB_COLUMN) values (1,TO_CLOB('... up to 4k of characters with quotes escaped ...')
|| TO_CLOB('... up to 4k of characters with quotes escaped ...')
|| TO_CLOB('... up to 4k of characters with quotes escaped ...')
...
|| TO_CLOB('... up to 4k of characters with quotes escaped ...'));
答案 1 :(得分:1)
以下EXP_IMP_LOB包可以使用简单的SQL(文本)文件导出和导入CLOB,NCLOB,BLOB类型的列数据。
首先将软件包安装到源模式和目标模式上。 要导出运行此选择
select * from table( EXP_IMP_LOB.EXPORT('table_name','lob_column_name','condition') );
其中Table_Name和LOB_Column_Name定义数据列,可选的Condition定义一行或多行。 如果没有条件,那么每行数据将逐行导出。
select * from table( EXP_IMP_LOB.EXPORT('person','image','id=103' ) );
/******************************************************
TABLE :PERSON
COLUMN :IMAGE
ROW :103
******************************************************/
BEGIN
EXP_IMP_LOB.IMPORT_NEW;
EXP_IMP_LOB.IMPORT_APPEND ( 'FFD8FFE000104A464....23232323232');
EXP_IMP_LOB.IMPORT_APPEND ( '32323232323232323....798999AA2A3');
.........
EXP_IMP_LOB.IMPORT_APPEND ( 'B2316524267279AA9....51401FFFD9');
EXP_IMP_LOB.IMPORT_UPDATE ( 'PERSON','IMAGE','103' );
COMMIT;
END;
/
因此,导出将二进制数据转换为400个字符长度的六进制字符串并从中创建脚本。 我使用.....来表示许多字符,因为这只是上面的一个例子。
要导入,您只需将软件包安装到目标模式上,然后在目标模式中运行上面的脚本。 这就是全部。
/*============================================================================================*/
create or replace package EXP_IMP_LOB is
/*============================================================================================*/
type T_STRING_LIST is table of varchar2( 32000 );
---------------------------------------------------------------------------
function EXPORT ( I_TABLE_NAME in varchar2
, I_COLUMN_NAME in varchar2
, I_WHERE in varchar2 default null
) return T_STRING_LIST pipelined;
---------------------------------------------------------------------------
---------------------------------------------------------------------------
procedure IMPORT_NEW;
---------------------------------------------------------------------------
---------------------------------------------------------------------------
procedure IMPORT_APPEND ( I_RAW in varchar2);
---------------------------------------------------------------------------
---------------------------------------------------------------------------
procedure DIRECT_SQL ( I_SQL in varchar2 );
---------------------------------------------------------------------------
---------------------------------------------------------------------------
procedure IMPORT_UPDATE ( I_TABLE_NAME in varchar2
, I_COLUMN_NAME in varchar2
, I_PK in varchar2
);
---------------------------------------------------------------------------
end;
/
/*============================================================================================*/
create or replace package body EXP_IMP_LOB is
/*============================================================================================*/
G_TABLE_NAME varchar( 40 );
G_COLUMN_NAME varchar( 40 );
G_COLUMN_TYPE varchar( 40 );
G_PK_KEY varchar( 4000 );
G_PK_LST varchar( 4000 );
G_LENGTH number := 200;
G_BLOB blob;
G_CLOB clob;
---------------------------------------------------------------------------
procedure GET_PK ( I_TABLE_NAME in varchar ) is
---------------------------------------------------------------------------
L_SEP varchar ( 40 ) := ',';
L_DATA_TYPE varchar2( 30 );
begin
G_PK_KEY := '';
G_PK_LST := '';
for L_A_PK in ( select COLUMN_NAME
from USER_CONSTRAINTS UC
, USER_CONS_COLUMNS DBC
where UC.CONSTRAINT_TYPE = 'P'
and DBC.CONSTRAINT_NAME = UC.CONSTRAINT_NAME
and DBC.TABLE_NAME = I_TABLE_NAME
order by position
)
loop
if nvl( length( G_PK_KEY ), 0 ) + length( L_A_PK.COLUMN_NAME ) < 4000 then
select DATA_TYPE into L_DATA_TYPE from user_tab_columns where table_name = G_TABLE_NAME and column_name = L_A_PK.COLUMN_NAME;
if instr( L_DATA_TYPE, 'CHAR') > 0 then
G_PK_KEY := G_PK_KEY||'''''''''||'||L_A_PK.COLUMN_NAME||'||''''''''||'''||L_SEP||'''||';
elsif instr( L_DATA_TYPE, 'DATE') > 0 then
G_PK_KEY := G_PK_KEY||'''TO_DATE(''''''||TO_CHAR('||L_A_PK.COLUMN_NAME||',''YYYY.MM.DD HH24:MI:SS'')||'''''',''''YYYY.MM.DD HH24:MI:SS'''')''||'''||L_SEP||'''||';
else
G_PK_KEY := G_PK_KEY||L_A_PK.COLUMN_NAME||'||'''||L_SEP||'''||';
end if;
G_PK_LST := G_PK_LST||L_A_PK.COLUMN_NAME||L_SEP;
end if;
end loop;
G_PK_KEY := substr( G_PK_KEY, 1, length( G_PK_KEY ) - ( 6 + length( L_SEP ) ) );
G_PK_LST := substr( G_PK_LST, 1, length( G_PK_LST ) - length(L_SEP));
end;
---------------------------------------------------------------------------
function EXPORT ( I_TABLE_NAME in varchar2
, I_COLUMN_NAME in varchar2
, I_WHERE in varchar2 default null
) return T_STRING_LIST pipelined is
---------------------------------------------------------------------------
V_BLOB blob;
V_CLOB clob;
V_CUR_SQL varchar( 32000 );
V_LOB_SQL varchar( 32000 );
V_RAW varchar( 32000 );
V_START number;
V_PK varchar( 4000 );
V_REC_SET sys_refcursor;
begin
G_TABLE_NAME := upper( trim( I_TABLE_NAME ) );
G_COLUMN_NAME := upper( trim( I_COLUMN_NAME ) );
GET_PK( G_TABLE_NAME );
select DATA_TYPE into G_COLUMN_TYPE from user_tab_columns where table_name = G_TABLE_NAME and column_name = G_COLUMN_NAME;
if G_COLUMN_TYPE not in ('CLOB','NCLOB','BLOB') then
raise_application_error ( -20001, 'The type of column '||I_COLUMN_NAME||' is not CLOB, NCLOB or BLOB' );
end if;
V_CUR_SQL := 'select '||G_PK_KEY||' from '||G_TABLE_NAME||' where '||nvl( I_WHERE, ' 1 = 1 ');
open V_REC_SET for V_CUR_SQL;
loop
fetch V_REC_SET into V_PK;
exit when V_REC_SET%notfound;
PIPE ROW( '/******************************************************' );
PIPE ROW( ' TABLE :'||G_TABLE_NAME );
PIPE ROW( ' COLUMN :'||G_COLUMN_NAME );
PIPE ROW( ' ROW :'||V_PK );
PIPE ROW( '******************************************************/' );
PIPE ROW( 'BEGIN' );
PIPE ROW( ' EXP_IMP_LOB.IMPORT_NEW;' );
V_LOB_SQL := 'select '||G_COLUMN_NAME||' from '||G_TABLE_NAME||' where ('||G_PK_LST||') in ( select '||V_PK||' from dual )';
if G_COLUMN_TYPE = 'BLOB' then
execute immediate V_LOB_SQL into V_BLOB;
if nvl( dbms_lob.getlength( V_BLOB ), 0 ) > 0 then
V_START := 1;
for L_I IN 1..ceil( dbms_lob.getlength( V_BLOB ) / G_LENGTH )
loop
V_RAW := dbms_lob.substr( V_BLOB, G_LENGTH, V_START );
PIPE ROW( ' EXP_IMP_LOB.IMPORT_APPEND ( '''||V_RAW||''');' );
V_START := V_START + G_LENGTH;
end loop;
PIPE ROW( ' EXP_IMP_LOB.IMPORT_UPDATE ( '''||G_TABLE_NAME||''','''||G_COLUMN_NAME||''','''||replace(V_PK,'''','''''')||''' ); ');
PIPE ROW( ' COMMIT;' );
end if;
else
execute immediate V_LOB_SQL into V_CLOB;
if nvl( dbms_lob.getlength( V_CLOB ), 0 ) > 0 then
V_START := 1;
for L_I IN 1..ceil( dbms_lob.getlength( V_CLOB ) / G_LENGTH )
loop
V_RAW := UTL_RAW.CAST_TO_RAW( dbms_lob.substr( V_CLOB, G_LENGTH, V_START ) );
PIPE ROW( ' EXP_IMP_LOB.IMPORT_APPEND ( '''||V_RAW||''');' );
V_START := V_START + G_LENGTH;
end loop;
PIPE ROW( ' EXP_IMP_LOB.IMPORT_UPDATE ( '''||G_TABLE_NAME||''','''||G_COLUMN_NAME||''','''||replace(V_PK,'''','''''')||''' ); ');
PIPE ROW( ' COMMIT;' );
end if;
end if;
PIPE ROW( 'END;' );
PIPE ROW( '/' );
PIPE ROW( ' ' );
end loop;
close V_REC_SET;
return;
end;
---------------------------------------------------------------------------
procedure IMPORT_NEW is
---------------------------------------------------------------------------
begin
G_BLOB := null;
G_CLOB := null;
end;
---------------------------------------------------------------------------
procedure IMPORT_APPEND ( I_RAW in varchar2 ) is
---------------------------------------------------------------------------
V_BLOB blob;
begin
V_BLOB := hextoraw( I_RAW );
if nvl( dbms_lob.getlength( V_BLOB ), 0 ) > 0 then
if nvl( dbms_lob.getlength( G_BLOB ), 0 ) = 0 then
G_BLOB := V_BLOB;
else
DBMS_LOB.APPEND( G_BLOB, V_BLOB );
end if;
end if;
end;
---------------------------------------------------------------------------
procedure DIRECT_SQL ( I_SQL in varchar2 ) is
---------------------------------------------------------------------------
begin
if nvl( dbms_lob.getlength( G_BLOB ), 0 ) > 0 then
execute immediate I_SQL using G_BLOB;
else
execute immediate I_SQL using G_CLOB;
end if;
commit;
end;
-- I downloaded this from the Net:
function clobfromblob( p_blob blob ) return clob is
l_clob clob;
l_dest_offsset integer := 1;
l_src_offsset integer := 1;
l_lang_context integer := dbms_lob.default_lang_ctx;
l_warning integer;
begin
if p_blob is null then
return null;
end if;
dbms_lob.createTemporary(lob_loc => l_clob
,cache => false);
dbms_lob.converttoclob(dest_lob => l_clob
,src_blob => p_blob
,amount => dbms_lob.lobmaxsize
,dest_offset => l_dest_offsset
,src_offset => l_src_offsset
,blob_csid => dbms_lob.default_csid
,lang_context => l_lang_context
,warning => l_warning);
return l_clob;
end;
---------------------------------------------------------------------------
procedure IMPORT_UPDATE ( I_TABLE_NAME in varchar2
, I_COLUMN_NAME in varchar2
, I_PK in varchar2
) is
---------------------------------------------------------------------------
V_SQL varchar( 32000 );
begin
G_TABLE_NAME := upper( trim( I_TABLE_NAME ) );
G_COLUMN_NAME := upper( trim( I_COLUMN_NAME ) );
GET_PK( G_TABLE_NAME );
select DATA_TYPE into G_COLUMN_TYPE from user_tab_columns where table_name = G_TABLE_NAME and column_name = G_COLUMN_NAME;
V_SQL := 'update '||I_TABLE_NAME||' set '||I_COLUMN_NAME||' = :1 where ('||G_PK_LST||') in ( select '||I_PK||' from dual )';
if G_COLUMN_TYPE in ( 'CLOB', 'NCLOB' ) then
G_CLOB := clobfromblob ( G_BLOB );
G_BLOB := null;
DIRECT_SQL( V_SQL );
elsif G_COLUMN_TYPE in ( 'BLOB' ) then
DIRECT_SQL( V_SQL );
end if;
end;
end;
/
答案 2 :(得分:0)
当我尝试将具有CLOB列的表从一个数据库服务器复制到另一个数据库服务器时,我遇到了这个问题。 Oracle SQL Developer的“工具&gt;复制数据库”工具对我来说不是一个好选择,因为我需要为目标表创建另一个名称,因此我必须使用“工具&gt;导出数据库”为您生成插入语句如果你选择的话。在.sql文件中。
在源数据库中运行:
SELECT
someColumA,
someColumB,
SubStr(myClobColumn, 1, 4000) myClobColumn_part1,
SubStr(myClobColumn, 4001, 8000) myClobColumn_part2,
. . .
SubStr(Clob_field, .., ..) Clob_field_partN,
OtherColumns
FROM YourTable ;
右键单击并导出到文件,所有insert语句都将写入临时表:EXPORT_TABLE。
在目标数据库服务器中运行该文件以创建EXPORT_TABLE
现在,运行此命令导入前4000个字符:
Insert Into YourDestinationTable(.., myClobColumn, ..)
SELECT .., myClobColumn_part1, ..
FROM EXPORT_TABLE ;
运行此选项以附加剩余的Clob部分:
Update YourDestinationTable A
set myClobColumn = myClobColumn
|| (Select myClobColumn_part2
From EXPORT_TABLE B
Where B.tableKey = A.tableKey
and myClobColumn_part2 is not null)
;
Update YourDestinationTable A
set myClobColumn = myClobColumn
|| (Select myClobColumn_part3
From EXPORT_TABLE B
Where B.tableKey = A.tableKey
and myClobColumn_part3 is not null)
;
...
直到N部分。
你已经完成了。
答案 3 :(得分:0)
当我尝试导出插入,clob或blob字段然后它不包含在插入查询中时,我遇到同样的问题。
由于插入限制对clob数据类型大小无效,这是问题。
解决方案:
我以XML或PDF或loader或Json导出数据。然后我得到了clob或blob的数据。
工具&gt;数据库导出&gt;选择连接&gt;如果您只想要数据&gt;取消选中导出DDl检查导出数据&gt;选择格式:XML&gt;下一个&gt;下一个&gt;下一个&gt;下一个&gt;完成。
打开保存该XML文件并验证的文件。
...享受