以下代码使用外部表时出错

error in below code using external tables

当我运行下面的代码时,它给了我以下错误:

ORA-00905: missing keyword
ORA-06512: at line 4

declare  
v_file varchar2(100):= 'samplefile.txt';  
begin  
execute immediate 'create table ext_tab2 (  
mprn char(10))  
ORGANIZATION EXTERNAL (  
  TYPE ORACLE_LOADER  
  DEFAULT DIRECTORY IMPORT  
    ACCESS PARAMETERS (  
    RECORDS DELIMITED BY NEWLINE  
    FIELDS TERMINATED BY '||','||'  
    (mprn char(10)))    
    LOCATION ('||v_file||')  
    ) 
    PARALLEL 5
REJECT LIMIT UNLIMITED';  
end;   

谁能告诉我错误在哪里?

您需要在逗号分隔值两边包含转义引号:

FIELDS TERMINATED BY '''||','||'''

或者更简单一点:

FIELDS TERMINATED BY '',''

还有文件名:

LOCATION ('''||v_file||''')

所以放在一起:

declare
v_file varchar2(100):= 'samplefile.txt';
begin
execute immediate 'create table ext_tab2 (
mprn char(10))
ORGANIZATION EXTERNAL (
TYPE ORACLE_LOADER
DEFAULT DIRECTORY IMPORT
ACCESS PARAMETERS (
RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY '''||','||'''
(mprn char(10)))
LOCATION ('''||v_file||''')
) PARALLEL 5 REJECT LIMIT UNLIMITED';
end;
/

anonymous block completed

desc ext_tab2

Name Null Type     
---- ---- -------- 
MPRN      CHAR(10) 

当您遇到动态语句问题时,显示生成的语句通常很有用(例如使用 dbms_output);像缺少空格这样的简单错误通常更加明显,当它不太明显时,您可以手动 运行 语句以获得更具体或有用的错误 - 指出 SQL 中的错误点,而不是 execute immediate.