我创建了一个流程代码,用于将数据从CSV文件导入Apex表。
The following code i have used in process:
DECLARE
v_blob_data BLOB;
v_blob_len NUMBER;
v_position NUMBER;
v_raw_chunk RAW(10000);
v_char CHAR(1);
c_chunk_len number := 1;
v_line VARCHAR2 (32767) := NULL;
v_data_array wwv_flow_global.vc_arr2;
v_line_count number := 0;
BEGIN
-- Read data from wwv_flow_files
select blob_content into v_blob_data
from wwv_flow_files
where CREATED_ON = (select max(CREATED_ON) from wwv_flow_files where lower(CREATED_BY) =lower(:APP_USER));
v_blob_len := dbms_lob.getlength(v_blob_data);
v_position := 1;
-- Read and convert binary to char
WHILE ( v_position <= v_blob_len ) LOOP
v_raw_chunk := dbms_lob.substr(v_blob_data,c_chunk_len,v_position);
v_char := chr(hex_to_decimal(rawtohex(v_raw_chunk)));
v_line := v_line || v_char;
v_position := v_position + c_chunk_len;
-- When a whole line is retrieved
IF v_char = CHR(10) THEN
v_line_count := v_line_count + 1;
-- Convert comma to : to use wwv_flow_utilities
v_line := REPLACE (v_line, ',', ':');
-- Convert each column separated by : into array of data
v_data_array := wwv_flow_utilities.string_to_table (v_line);
-- Insert data into target table
IF v_line_count > 1 THEN
EXECUTE IMMEDIATE 'insert into NON_DYNAMIC_USER_GROUPS_TEMP (ID,WORKSPACES,GROUP_NAME,MEMBERS) values (NON_DYNAMIC_GROUPS_TEMP_SEQ.NEXTVAL,(:1),(:2),(:3))'
USING
v_data_array(1), v_data_array(2), v_data_array(3);
end if;
-- Clear out
v_line := NULL;
END IF;
END LOOP;
END;
Here for 3rd column i have to insert multiple values with comma separator from csv file, for ex in v_data_array(3) i have to entered ABINNAYA,BARATH,CHELLA this has to insert in table as ABINNAYA,BARATH,CHELLA.
insert into table工作正常,但是如果第3列有多个值,则只会插入多个值。
Thanks
答案 0 :(得分:0)
如果您使用OraOpenSource Utils套餐,可以使用更优雅的方式将 CSV 数据插入到表格中。
使用它,您可以使用类似于此的块代码将数据插入表中:
declare
v_blob_data blob;
v_clob_data clob;
-- variables needed for blob-clob convertion
v_dest_offset integer := 1;
v_src_offset integer := 1;
v_lang_context INTEGER := DBMS_LOB.default_lang_ctx;
v_warning integer := 0;
-- one line of the CSV data
v_line oos_util.tab_vc2_arr;
begin
-- load CSV data into blob var
select blob_content into v_blob_data
from wwv_flow_files
where CREATED_ON = (select max(CREATED_ON) from wwv_flow_files where lower(CREATED_BY) =lower(:APP_USER));
-- converting blob to clob
dbms_lob.createtemporary(v_clob_data, true);
dbms_lob.converttoclob(v_clob_data, v_blob_data, DBMS_LOB.LOBMAXSIZE, v_dest_offset, v_src_offset, nls_charset_id('UTF8'), v_lang_context, v_warning);
-- splitting the CSV to strings and then inserting each row attribute by attribute
for x in (
select column_value val from table(oos_util_string.listunagg(p_str => v_clob_data, p_delim => chr(10)))
) loop
v_line := oos_util_string.string_to_table(x.val);
-- you can apply some datatype conversion here if needed (for example with to_date() in case there are date columns)
insert into ttt values (v_line(1), v_line(2), v_line(3));
end loop;
commit;
end;
首先,它将您的数据加载到BLOB变量中,然后转换为CLOB变量(请注意代码页),然后使用OOS Utils将数据首先拆分为行,最后按属性属性将数据插入目标表。
执行数据类型转换时要准确。
P.S。在您的原始帖子中,您真的需要使用动态SQL来完成您的任务吗?它似乎不是这样。
我不确定我是否回答了你的问题,因为我不清楚你的问题是什么。