plsql - Creating external tables in Oracle procedure -


i'm using oracle 11g , i'm having in issue creating external table in procedure. gets created no errors when execute procedure i'm having errors.

the first parameter name of file , second comma because having issues using single quotations surround comma specify fields terminated section. data_dir declared.

here's tried.

create or replace procedure loadtable (     filename varchar2,     comma varchar ) begin      execute immediate 'create table load     (        username varchar2(30)     )     organization external     (         type oracle_loader         default directory data_dir         access parameters         ( fields terminated :comma)         location (:filename)     )' using in comma, filename; end; 

this how call procedure

exec loadtable('username.csv',','); 

this error

error @ line 1: ora-00931: missing identifier ora-06512: @ "data_admin.loadtable", line 9 ora-06512: @ line 1 

any appreciated.

you can bind variables, , external table creation syntax requires text literals elements you're trying bind.

you'll have use concatenation instead:

create or replace procedure loadtable (   filename varchar2,   comma varchar ) begin   execute immediate 'create table load (   username varchar2(30) ) organization external (   type oracle_loader   default directory data_dir   access parameters   (fields terminated ''' || comma || ''')   location (''' || filename || ''') )'; end; /  procedure loadtable compiled  exec loadtable('username.csv',',');  pl/sql procedure completed. 

in i've escaped single quotes around concatenated string values. in question mentioned you're passing comma because "having issues using single quotations surround comma"; escaping them doubling them way that, if want comma separator can instead do:

create or replace procedure loadtable (   filename varchar2 ) begin   execute immediate 'create table load (   username varchar2(30) ) organization external (   type oracle_loader   default directory data_dir   access parameters   (fields terminated '','')   location (''' || filename || ''') )'; end; /  exec loadtable('username.csv'); 

however, creating (and presumably dropping) objects on fly isn't idea. better create external table once, done static ddl:

create table load (   username varchar2(30) ) organization external (   type oracle_loader   default directory data_dir   access parameters   (fields terminated ',')   location ('dummy') ); 

and alter table have new file name statically too:

alter table load location ('username.csv'); 

or if want procedure it:

create or replace procedure loadtable (   filename varchar2 ) begin   execute immediate 'alter table load location (''' || filename || ''')'; end; /  exec loadtable('username.csv'); 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -