This i how i did the dynamic query and pass the output of a select statement from an execute immediate into variables
SET ECHO OFF
SET SERVEROUTPUT ON SIZE 1000000
SET FEEDBACK ON
SET TIMING ON
DECLARE
mysql_statement clob;
var_record_count number;
var_field1 number;
var_field2 varchar2(30 CHAR);
Begin
for my_list in (
select 1 as record_count, 101 as field1, 'record number 1' as field2 from dual union
select 2 as record_count, 102 as field1, 'record number 2' as field2 from dual union
select 3 as record_count, 103 as field1, 'record number 3' as field2 from dual
)
LOOP
/*-- build your dynmic sql and assign to a variable --*/
mysql_statement := 'select '||my_list.record_count||', '||my_list.field1||', '''||my_list.field2||''' from dual';
/*-- display your dynamic sql--*/
DBMS_OUTPUT.PUT_LINE(mysql_statement);
/*-- execute whatever you sql is using execute immediate and pass the result for each list into your variables --*/
execute Immediate mysql_statement into var_record_count, var_field1 ,var_field2;
/*-- display your variables --*/
DBMS_OUTPUT.PUT_LINE(var_record_count||'|'||var_field1||'|'||var_field2);
END LOOP;
END;
/
show errors;
--- output --
anonymous block completed
Elapsed: 00:00:00.194
select 1, 101, 'record number 1' from dual
1|101|record number 1
select 2, 102, 'record number 2' from dual
2|102|record number 2
select 3, 103, 'record number 3' from dual
3|103|record number 3
No Errors.
SET ECHO OFF
SET SERVEROUTPUT ON SIZE 1000000
SET FEEDBACK ON
SET TIMING ON
DECLARE
mysql_statement clob;
var_record_count number;
var_field1 number;
var_field2 varchar2(30 CHAR);
Begin
for my_list in (
select 1 as record_count, 101 as field1, 'record number 1' as field2 from dual union
select 2 as record_count, 102 as field1, 'record number 2' as field2 from dual union
select 3 as record_count, 103 as field1, 'record number 3' as field2 from dual
)
LOOP
/*-- build your dynmic sql and assign to a variable --*/
mysql_statement := 'select '||my_list.record_count||', '||my_list.field1||', '''||my_list.field2||''' from dual';
/*-- display your dynamic sql--*/
DBMS_OUTPUT.PUT_LINE(mysql_statement);
/*-- execute whatever you sql is using execute immediate and pass the result for each list into your variables --*/
execute Immediate mysql_statement into var_record_count, var_field1 ,var_field2;
/*-- display your variables --*/
DBMS_OUTPUT.PUT_LINE(var_record_count||'|'||var_field1||'|'||var_field2);
END LOOP;
END;
/
show errors;
--- output --
anonymous block completed
Elapsed: 00:00:00.194
select 1, 101, 'record number 1' from dual
1|101|record number 1
select 2, 102, 'record number 2' from dual
2|102|record number 2
select 3, 103, 'record number 3' from dual
3|103|record number 3
No Errors.
Comments
Post a Comment