I'm using Oracle 11.g I'm having trouble getting Dynamic SQL to work in a particular scenario. I have an array of Authors with the First Name and the Last Name separated by a single space. I need to separate the FN and LN into two field as I loop through the array of authors.
declare
l_pls_block varchar2(512);
l_curr_author varchar2(512);
l_curr_author_ln_varname varchar2(512);
l_curr_author_ln_value varchar2(512);
BEGIN
For k in 1 .. 3
loop
l_curr_author := 'Joe Schmoe';
l_curr_author_ln_varname := 'l_P133_AUTHOR' || k || '_LAST_NAME';
l_pls_block := l_curr_author_ln_varname ||
' := trim(substr(l_curr_author, (instr(l_curr_author, '' '')+1)))';
dbms_output.put_line('l_pls_block is: ' || l_pls_block);
execute immediate l_pls_block;
dbms_output.put_line('l_P133_AUTHORx_LAST_NAME is: ' ||
l_curr_author_ln_varname);
end loop;
end;
The above code returns: ORA-00900: invalid SQL statement ORA-06512: at line 14 I've tried using "Execute Immediate l_pls_block into xxxx" but I can't get that to work. As a test, I've constructed the statement:
my_test_val := trim(substr(l_curr_author, (instr(l_curr_author, ' ')+1)))';
and that works fine. How can I using Dynamic SQL to referencing changing variable names to obtain a substring of another value? Thanks for looking at this.