plsql - How can I use Oracle (PL/SQL) dynamic sql to query data into a %rowtype variable -


the problem:

i have table contains clob fixed length record external source. positions 1-5 = fielda, positions 6-12 = fieldb, etc.

i have table (layoutdefinition)that defines record layout,

fieldname = 'fielda' fieldstart = 1 fieldlength = 5 fieldname = 'fieldb' fieldstart = 6 fieldlength = 6 

i need retrieve data clob , put %rowtype variable such "tablea_rec tablea%rowtype."

i have implemented routine uses massive case statement, , loop each row in layoutdefiniton table moves area of clob proper variable in tablea_rec like;

case layoutdefiniton.fieldname when 'fielda' tablea_rec.fielda:= substr(inputclob,layoutdefiniton.fieldstart,layoutdefiniton.fieldlength); when 'fieldb' tablea_rec.fieldb:= substr(inputclob,layoutdefiniton.fieldstart,layoutdefiniton.fieldlength); 

this of course inefficient, need loop through layout each record picking apart data.

what create dynamic sql select statement once retrieve data table proper variables. example if not dynamic might like;

select substr(inputclob,1,5), substr(inputclob,6,6) fielda, fieldb clobtable; 

can done using dynamic sql?

if syntax like?

the following code extract data clob column , insert destination table

declare   l_sql_str varchar2(4000); begin   src_meta_agg as(     select listagg(field_name, ',') within group(order field_start) field_list            ,listagg('substr(lob,' || field_start || ', ' || field_length || ')', ',') within group( order field_start) field_source       src_meta)     select 'insert dest(' || field_list || ') select ' || field_source ||            ' src' l_sql_str       src_meta_agg;    execute immediate l_sql_str; end; 

listagg 11g function orders data , concatenates values. previous versions can use wm_concat or other approach. in l_sql_str have following statement

insert dest(field1,field2) select substr(lob,1, 5),substr(lob,6, 6) src 

if need process data in pl/sql code trick

declare   l_sql_str varchar2(4000);   type t_dest_tbl table of dest%rowtype;   l_dest_rows t_dest_tbl;   l_cursor sys_refcursor; begin    src_meta_agg as(     select listagg(field_name, ',') within group(order field_start) field_list            ,listagg('substr(lob,' || field_start || ', ' || field_length || ')', ',') within group( order field_start) field_source       src_meta)     select 'select ' || field_source ||            ' src' l_sql_str       src_meta_agg;    open l_cursor l_sql_str;   fetch l_cursor   bulk collect l_dest_rows;   close l_cursor;     in 1..l_dest_rows.count loop     dbms_output.put_line(l_dest_rows(i).field1 || ', ' || l_dest_rows(i).field2);   end loop; end; 

here schema used

create table src(lob clob) / insert src values ('12345abcdef') / insert src values ('78901ghijkl') /  create table src_meta(field_name varchar2(100), field_start number, field_length number) /  insert src_meta values ('field1', 1, 5) / insert src_meta values ('field2', 6, 6) /  create table dest(field1 varchar2(5), field2 varchar2(6)) / 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -