dbms_sql在dbms.parse()处引发错误。
[格式错误]
尝试将CSV文件上传到Oracle 12c数据库表时遇到此错误。它在dbms_sql.parse()函数中失败,因为我将插入查询作为参数而不是
load_csv代码已附加。
create or replace FUNCTION load_csv ( p_table in varchar2,
p_dir in varchar2 DEFAULT 'THINS_UPD' ,
P_FILENAME in varchar2 ,
p_ignore_headerlines IN INTEGER DEFAULT 1,
p_delimiter in varchar2 default ',',
p_optional_enclosed in varchar2 default '"' )
return number
is
/***************************************************************************
-- PROCEDURE LOAD_CSV
-- PURPOSE: This Procedure read the data from a CSV file.
-- And load it into the target oracle table.
-- Finally it renames the source file with date.
--
-- P_FILENAME
-- The name of the flat file(a text file)
--
-- P_DIRECTORY
-- Name of the directory where the file is been placed.
-- Note: The grant has to be given for the user to the directory
-- before executing the function
--
-- P_IGNORE_HEADERLINES:
-- Pass the value as '1' to ignore importing headers.
--
-- P_DELIMITER
-- By default the delimiter is used as ','
-- As we are using CSV file to load the data into oracle
--
-- P_OPTIONAL_ENCLOSED
-- By default the optionally enclosed is used as '"'
-- As we are using CSV file to load the data into oracle
--
**************************************************************************/
l_input utl_file.file_type;
l_lastLine varchar2(4000);
l_cnames varchar2(4000);
l_bindvars varchar2(4000);
l_status integer;
l_cnt number default 0;
l_rowCount number default 0;
l_sep char(1) default NULL;
L_ERRMSG varchar2(4000);
V_EOF BOOLEAN := false;
l_theCursor number default dbms_sql.open_cursor;
v_insert varchar2(1100);
begin
l_cnt := 1;
for TAB_COLUMNS in (
select column_name,data_type from user_tab_columns where table_name=p_table order by column_id
) loop
l_cnames := l_cnames || tab_columns.column_name || ',';
--Dbms_Output.put_line(tab_columns.column_name ||'colname');
-- l_bindvars := l_bindvars || case when tab_columns.data_type in ('DATE', 'TIMESTAMP(6)') then 'to_date(:b' || l_cnt || ',"YYYY-MM-DD HH24:MI:SS"),' else ':b'|| l_cnt || ',' end;
l_bindvars := l_bindvars || case when tab_columns.data_type in ('DATE', 'TIMESTAMP(6)') then ':b'|| l_cnt || ',' else ':b'|| l_cnt || ',' end;
l_cnt := l_cnt + 1;
end loop;
l_cnames := rtrim(l_cnames,',');
L_BINDVARS := RTRIM(L_BINDVARS,',');
L_INPUT := UTL_FILE.FOPEN( P_DIR, P_FILENAME, 'r' );
IF p_ignore_headerlines > 0
THEN
BEGIN
FOR i IN 1 .. p_ignore_headerlines
LOOP
UTL_FILE.get_line(l_input, l_lastLine);
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
v_eof := TRUE;
end;
END IF;
v_insert :='insert into ' || p_table || '(' || l_cnames || ') values (' || l_bindvars || ')';
Dbms_Output.put_line(l_theCursor||'cursor'||l_bindvars||'---'||l_cnames);
if not v_eof THEN
Dbms_Output.put_line(l_theCursor||'-'||'insert into ' || p_table || '(' || l_cnames || ') values (' || l_bindvars || ')');--Akash added
--dbms_sql.parse( l_theCursor, 'insert into ' || p_table || '(' || l_cnames || ') values (' || l_bindvars || ');' dbms_sql.native );
DBMS_SQL.parse(l_theCursor ,v_insert, dbms_sql.native );
Dbms_Output.put_line(112);
loop
begin
utl_file.get_line( l_input, l_lastLine );
exception
when NO_DATA_FOUND then
exit;
end;
if length(l_lastLine) > 0 then
for i in 1 .. l_cnt-1
LOOP
dbms_sql.bind_variable( l_theCursor, ':b'||i,
rtrim(rtrim(ltrim(ltrim(
REGEXP_SUBSTR(l_lastline,'(^|,)("[^"]*"|[^",]*)',1,i),p_delimiter),p_optional_enclosed),p_delimiter),p_optional_enclosed));
end loop;
begin
l_status := dbms_sql.execute(l_theCursor);
l_rowCount := l_rowCount + 1;
exception
when OTHERS then
L_ERRMSG := SQLERRM;
insert into BADLOG ( TABLE_NAME, ERRM, data, ERROR_DATE )
values ( P_TABLE,l_errmsg, l_lastLine ,systimestamp );
end;
end if;
end loop;
dbms_sql.close_cursor(l_theCursor);
utl_file.fclose( l_input );
commit;
end if;
insert into IMPORT_HIST (FILENAME,TABLE_NAME,NUM_OF_REC,IMPORT_DATE)
values ( P_FILENAME, P_TABLE,l_rowCount,sysdate );
Dbms_Output.put_line(P_DIR||'-'||P_FILENAME);--added by Akash
RETURN L_ROWCOUNT;
end LOAD_CSV;