Defining Collections with variables - oracle

How can I define Collections in PL/SQL with variables?
Eg.
v_owner varchar(128) := 'SCHEMA_USER';
v_tablename varchar(128) := 'TABLENAME';
TYPE t_tab IS TABLE OF SCHEMA_USER.TABLENAME%ROWTYPE;
v_tab t_tab;
What I want is to use the variables instead of the names of the owner/table.
Something like that:
TYPE t_tab IS TABLE OF v_owner.v_tablename%ROWTYPE;
But that does not work of course.
Any idea?

You need to use dynamic PL/SQL something like this:
SQL> DECLARE
2 v_owner varchar(128) := 'MYSCHEMA';
3 v_tablename varchar(128) := 'EMP';
4 v_str LONG;
5 BEGIN
6 v_str := 'DECLARE TYPE t_tab IS TABLE OF ' || v_owner || '.' || v_tablename || '%ROWTYPE;'
7 || ' v_tab t_tab;'
8 || ' BEGIN'
9 || ' SELECT * BULK COLLECT INTO v_tab'
10 || ' FROM emp WHERE empno = :input1;'
11 || ' dbms_output.put_line(v_tab(1).ename);'
12 || 'END;';
13 EXECUTE IMMEDIATE v_str USING 7839;
14 END;
15 /
KING
PL/SQL procedure successfully completed.

Thank you very much for the response.
I had to modify the whole thing a bit, but that worked:
DECLARE
v_owner varchar(128) := 'SCHEMA';
v_tablename varchar(128) := 'TABLE';
v_str LONG;
BEGIN
v_str := 'DECLARE TYPE t_tab IS TABLE OF ' || v_owner || '.' || v_tablename || '%ROWTYPE;'
|| ' v_tab t_tab;'
|| ' BEGIN'
|| ' SELECT * BULK COLLECT INTO v_tab'
|| ' FROM ' || v_owner || '.' || v_tablename ||';'
|| 'END;';
EXECUTE IMMEDIATE v_str;
END;
/

Related

Can we use collection variable as a table in execute immediate?

Declare
Type t_approved_node is record( node_rowid Hr
node_rowid%type, Node_+type hr.node_type%type);
Type t_val is table of t_approved_node Index by pls_integer;
V_node t_val;
V_tab varchar2(20);
V_col varchar2(400);
V_nrf_flg hr.hr_flag%type;
V_ubrf_flg hr.hr_flag%type := 3;
V_col_str varchar2(4000);
Begin
Begin
Select hr_flag into v_nrf_flg from hr;
End;
Begin
Select h.node_rowid, h.node_type bulk collect into v_node
from hr h, hr_attr_wfm haw
Where h.hr_relation_id = haw.uc_hr_relation_id
And h.node_type = 'UBR';
Begin
V_tab := 'UC_UBR';
Select listagg(column_name, ',' within group(order by
column_id)
Into v_col from user_tab_columns where table_name = v_tab;
End;
V_col_str := regex_replace( v_col, 'HR_FLAG', v_ubrf_flg);
Execute immediate ' insert into ' || v_tab || '( ' ||
V_col || ') ' || ' select '|| v_col_str || ' from ' ||
V_tab || 'R ' || q' [ where node_type = ' UBR' a
and hr_flag =:1 and exists( ] ' || ' select 1 ' || ' from table( ' ||
v_node || ')y' || q' [ where y.node_rowid = R.node_rowid ] )'
Using v_nrf_flag;
End;
End;
I was trying to execute above block getting below error.
Wrong number or types of arguments in call to ||
Final query should be like
insert into UC_UBR ( v_col)/*3 columns into v_col variable*/
select v_col_str /* 3 columns in v_col_str variable*/ from UC_UBR R where hr_flag =:1
and exists
(select 1 from table(v_node) /*collection variable*/ y
where y.node_rowid = r.node_rowod;
Can anyone help on this?
Your sample code is full of errors and does not make any sense at all. But if I focus on your question, then the answer is "yes". See this example:
CREATE OR REPLACE TYPE t_app AS OBJECT( nodeid NUMBER, Nodetype VARCHAR2(100));
CREATE OR REPLACE TYPE t_val IS TABLE OF t_app;
DECLARE
V_node t_val;
V_result t_val;
V_app t_app;
V_count NUMBER;
Sql_stmt VARCHAR2(100);
nodeid NUMBER;
Nodetype VARCHAR2(100);
BEGIN
SELECT t_app(nodeid, Nodetype) BULK COLLECT INTO V_node FROM HR;
Sql_stmt := 'SELECT count(*) FROM TABLE(:t)';
EXECUTE IMMEDIATE Sql_stmt INTO V_count USING V_node;
DBMS_OUTPUT.PUT_LINE ( 'V_count = ' || V_count );
Sql_stmt := 'SELECT nodeid, Nodetype FROM TABLE(:t) WHERE ROWNUM = 1';
EXECUTE IMMEDIATE Sql_stmt INTO nodeid, Nodetype USING V_node;
DBMS_OUTPUT.PUT_LINE ( 'nodeid = ' || nodeid );
DBMS_OUTPUT.PUT_LINE ( 'Nodetype = ' || Nodetype );
Sql_stmt := 'SELECT t_app(nodeid, Nodetype) FROM TABLE(:t) WHERE ROWNUM = 1';
EXECUTE IMMEDIATE Sql_stmt INTO V_app USING V_node;
DBMS_OUTPUT.PUT_LINE ( 'V_app = ' || XMLTYPE(V_app).getClobVal() );
Sql_stmt := 'SELECT t_app(nodeid, Nodetype) FROM TABLE(:t)';
EXECUTE IMMEDIATE Sql_stmt BULK COLLECT INTO V_result USING V_node;
END;
It would help if you posted real code you used, because this is full of syntax errors (missing sql_stmt local variable declaration, put.line (?)).
I have no idea what you plan to do with such a select statement as you can't execute it, it doesn't make any sense but - here you go; see line #20.
SQL> set serveroutput on
SQL>
SQL> DECLARE
2 TYPE t_app IS RECORD
3 (
4 nodeid NUMBER,
5 Nodetype VARCHAR2 (20)
6 );
7
8 TYPE t_val IS TABLE OF t_app
9 INDEX BY PLS_INTEGER;
10
11 V_node t_val;
12 V_tab VARCHAR2 (20);
13
14 sql_stmt VARCHAR2 (200);
15 BEGIN
16 SELECT empno, ename
17 BULK COLLECT INTO v_node
18 FROM emp;
19
20 Sql_stmt := 'select 1 from (' || v_node (1).nodeid || 'Y)';
21
22 DBMS_OUTPUT.put_line (sql_stmt);
23 END;
24 /
select 1 from (7369Y)
PL/SQL procedure successfully completed.
SQL>

Execute immediate in stored procedure: command not properly ended

I got this procedure:
create or replace procedure TEST_PROCEDURE(
time_in in varchar2,
repository_in in varchar2,
iteration_in in varchar2,
tar_table_in in varchar2
)
is
delete_stmt varchar2(2000);
insert_stmt varchar2(2000);
begin
delete_stmt := 'delete from ' || tar_table_in || ' where time=' || time_in ||' and repository=' || repository_in || '
and iteration=' || iteration_in || '; commit;';
execute immediate delete_stmt;
insert_stmt := 'insert into ' || tar_table_in || ' (some columns)
SELECT ' || repository_in || ' as repository, ' || iteration_in || ' as iteration, t1.* FROM dual
left join
json_table((select json_response from TEST_TABLE where repository=repository_in), ''$[*]''
COLUMNS
time varchar2(64) PATH ''$.time'',
session_id varchar2(256) PATH ''$.session_id''
) t1
on 1=1';
execute immediate insert_stmt;
end;
It currently throws an ORA-00933 error at the "execute immediate delete_stmt;" line, which hints to something foul in the query string.
I can't seem to find the location of either a missing quote, or semi-colon that would end the command. Anyone able to spot what I'm missing?
If you are using SQL Dynamic, then your values in your dynamic query are missing quotes.
Don't use commit inside execute immediate.
You do the delete, then the insert, then you commit the transaction.
It should be
create or replace procedure TEST_PROCEDURE(
time_in in varchar2,
repository_in in varchar2,
iteration_in in varchar2,
tar_table_in in varchar2
)
is
delete_stmt varchar2(2000);
insert_stmt varchar2(2000);
begin
delete_stmt := 'delete from ' || tar_table_in || ' where time= ''' || time_in || ''' and repository= ''' || repository_in || '''
and iteration = ''' || iteration_in || ''' ';
execute immediate delete_stmt;
insert_stmt := 'insert into ' || tar_table_in || ' (some columns)
SELECT ''' || repository_in || ''' as repository, ''' || iteration_in || ''' as iteration, t1.* FROM dual
left join
json_table((select json_response from TEST_TABLE where repository=repository_in), ''$[*]''
COLUMNS
time varchar2(64) PATH ''$.time'',
session_id varchar2(256) PATH ''$.session_id''
) t1
on 1=1';
execute immediate insert_stmt;
commit;
end;
Whenever trying to debug dynamic sql, assign the statement to a variable (which you do already) then use dbms_output to show the exact statement that will be executed:
SQL> create or replace procedure my_proc (time_in in varchar2,
2 repository_in in varchar2,
3 iteration_in in varchar2,
4 tar_table_in in varchar2)
5 as
6 v_sql varchar2(1000);
7 begin
8 v_sql:='delete from ' ||
9 tar_table_in ||
10 ' where time=' ||
11 time_in ||
12 ' and repository=' ||
13 repository_in || '
14 and iteration=' ||
15 iteration_in ||
16 '; commit;';
17
18 dbms_output.put_line('====== begin debug line ======');
19 dbms_output.put_line(v_sql);
20 dbms_output.put_line('====== end debug line ======');
21 end;
22 /
Procedure created.
SQL> show errors
No errors.
SQL> set serverout on
SQL> exec my_proc('aaaa','bbbb','cccc','dddd');
====== begin debug line ======
delete from dddd where time=aaaa and repository=bbbb
and iteration=cccc;
commit;
====== end debug line ======
PL/SQL procedure successfully completed.
SQL> --
SQL> drop procedure my_proc;
Procedure dropped.

ORA-00936: missing expression - Line 62

I've been trying to identify what's wrong with the Insert Statement in the Execute Immediate for few hours without luck. Made sure that I am not missing any commas or entering any incorrect character.
I have gone through all the answers on SO and other websites trying to figure out what could I be doing wrong but no luck.
Running this function results in the following error (error line starts with "-->" Please ignore it as its just for highlighting purpose):
ORA-00936: missing expression
ORA-06512: at "BDW_AMPS.COUNT_RECORDS", line 62
and here's the PL/SQL Code for the function:
CREATE OR REPLACE FUNCTION count_records (
p_test_case_id IN NUMBER,
p_table_name IN VARCHAR2
) RETURN VARCHAR2 IS
v_amt_recs INT;
v_test_result VARCHAR2(10);
v_threshold_val VARCHAR2(10);
v_test_suite_table VARCHAR2(100);
v_test_result_id NUMBER;
v_batch_id NUMBER;
v_report_id NUMBER;
v_test_seq_no NUMBER;
v_session_name VARCHAR2(100);
v_error_description VARCHAR2(100);
v_process_by VARCHAR2(100);
BEGIN
v_test_suite_table := 'bdw_amps.spares_bdw_test_suite';
v_process_by := 'INFORMATICA';
EXECUTE IMMEDIATE 'SELECT THRESHHOLD_VALUE FROM '
|| v_test_suite_table
|| ' WHERE TEST_CASE_ID = '
|| p_test_case_id
INTO v_threshold_val;
EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM ' || p_table_name
INTO v_amt_recs;
EXECUTE IMMEDIATE 'SELECT BDW_AMPS.SPARES_TEST_SEQ_ID_SEQ.NEXTVAL FROM DUAL'
INTO v_test_result_id;
EXECUTE IMMEDIATE 'SELECT MAX(BATCH_ID) FROM BDW_AMPS.spares_bdw_session_audit
WHERE SESSION_NAME=(SELECT SESSION_NAME FROM '
|| v_test_suite_table
|| ' WHERE TEST_CASE_ID = '
|| p_test_case_id
|| ')'
INTO v_batch_id;
EXECUTE IMMEDIATE 'SELECT REPORT_ID FROM '
|| v_test_suite_table
|| ' WHERE TEST_CASE_ID = '
|| p_test_case_id
INTO v_report_id;
EXECUTE IMMEDIATE 'SELECT TEST_SEQ FROM '
|| v_test_suite_table
|| ' WHERE TEST_CASE_ID = '
|| p_test_case_id
INTO v_test_seq_no;
EXECUTE IMMEDIATE 'SELECT SESSION_NAME FROM '
|| v_test_suite_table
|| ' WHERE TEST_CASE_ID = '
|| p_test_case_id
INTO v_session_name;
IF
v_amt_recs > v_threshold_val
THEN
v_test_result := 'PASS';
--> EXECUTE IMMEDIATE 'INSERT INTO BDW_AMPS.spares_bdw_test_results(
TEST_RESULT_ID,
BATCH_ID,
REPORT_ID,
TEST_CASE_ID,
TEST_SEQ_NO,
TABLE_NAME,
SESSION_NAME,
TEST_RESULT,
PROCESS_DATE,
PROCESS_BY
)
VALUES
('|| v_test_result_id || ',
' || v_batch_id || ',
' || v_report_id || ',
' || p_test_case_id || ',
' || v_test_seq_no || ',
' || p_table_name || ',
' || v_session_name || ',
' || v_test_result || ',
SYSDATE,
' || v_process_by || '
)';
EXECUTE IMMEDIATE 'commit';
ELSE
v_test_result := 'FAIL';
v_error_description := 'Count: ' || v_amt_recs || ' is greater than threshold value: ' || v_threshold_val;
EXECUTE IMMEDIATE 'INSERT INTO BDW_AMPS.spares_bdw_test_results(
TEST_RESULT_ID,
BATCH_ID,
REPORT_ID,
TEST_CASE_ID,
TEST_SEQ_NO,
TABLE_NAME,
SESSION_NAME,
TEST_RESULT,
ERROR_DESCRIPTION,
PROCESS_DATE,
PROCESS_BY
)
VALUES (
'|| v_test_result_id || ',
' || v_batch_id || ',
' || v_report_id || ',
' || p_test_case_id || ',
' || v_test_seq_no || ',
' || p_table_name || ',
' || v_session_name || ',
' || v_test_result || ',
' || v_error_description || ',
SYSDATE,
' || v_process_by || '
)';
EXECUTE IMMEDIATE 'commit';
END IF;
RETURN v_test_result;
END;
Assuming that this is a simplified example of something that really does need to be dynamic, one issue is that the string values are not quoted. (If you'd had date values they would need special handling too.)
For example:
create table demo (numcol number, stringcol varchar2(20));
declare
l_num number := 123;
l_string varchar2(20) := 'Kittens';
l_sql long := 'insert into demo(numcol, stringcol) values ('||l_num||', '||l_string||')';
begin
dbms_output.put_line(l_sql);
execute immediate l_sql;
end;
/
Generated code:
insert into demo(numcol, stringcol) values (123, Kittens)
Fails with:
ORA-00984: column not allowed here
The error will vary depending on the contents of the string. For example, if it contains spaces:
declare
l_num number := 123;
l_string varchar2(20) := 'Kittens are cute';
l_sql long := 'insert into demo(numcol, stringcol) values ('||l_num||', '||l_string||')';
begin
dbms_output.put_line(l_sql);
execute immediate l_sql;
end;
/
Generated code:
insert into demo(numcol, stringcol) values (123, Kittens are cute)
ORA-00917: missing comma
or commas:
declare
l_num number := 123;
l_string varchar2(20) := 'Kittens, Puppies';
l_sql long := 'insert into demo(numcol, stringcol) values ('||l_num||', '||l_string||')';
begin
dbms_output.put_line(l_sql);
execute immediate l_sql;
end;
/
insert into demo(numcol, stringcol) values (123, Kittens, Puppies)
ORA-00913: too many values
You need to build the quoting:
declare
l_num number := 123;
l_string varchar2(20) := 'Kittens, Puppies';
l_sql long := 'insert into demo(numcol, stringcol) values ('||l_num||', '''||l_string||''')';
begin
dbms_output.put_line(l_sql);
execute immediate l_sql;
end;
/
so that you generate
insert into demo(numcol, stringcol) values (123, 'Kittens, Puppies')
(If the string could contain quote characters, that would need more work.)
It's worth always building the dynamic SQL as a variable and printing or logging it on failure, as it's usually pretty clear what the issue is when you can see the code.
Another point is that concatenating values like this is resource-intensive, as Oracle tries to cache SQL statements for reuse, so they will be individually parsed and optimised and take space in the cache, but they will never be reused. If this is going to be frequently run with different values, you should consider using bind variables via the using clause of execute immediate.
Your DML(INSERT) statements do not need EXECUTE IMMEDIATE statements. So, remove them after line 61 :
CREATE OR REPLACE FUNCTION count_records (
p_test_case_id IN NUMBER,
p_table_name IN VARCHAR2
) RETURN VARCHAR2 IS
v_amt_recs INT;
v_test_result VARCHAR2(10);
v_threshold_val VARCHAR2(10);
v_test_suite_table VARCHAR2(100);
v_test_result_id NUMBER;
v_batch_id NUMBER;
v_report_id NUMBER;
v_test_seq_no NUMBER;
v_session_name VARCHAR2(100);
v_error_description VARCHAR2(100);
v_process_by VARCHAR2(100);
BEGIN
v_test_suite_table := 'bdw_amps.spares_bdw_test_suite';
v_process_by := 'INFORMATICA';
EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM ' || p_table_name
INTO v_amt_recs;
v_test_result_id := BDW_AMPS.SPARES_TEST_SEQ_ID_SEQ.NEXTVAL;
EXECUTE IMMEDIATE 'SELECT MAX(BATCH_ID) FROM BDW_AMPS.spares_bdw_session_audit
WHERE SESSION_NAME=(SELECT SESSION_NAME FROM '
|| v_test_suite_table
|| ' WHERE TEST_CASE_ID = '
|| p_test_case_id
|| ')'
INTO v_batch_id;
EXECUTE IMMEDIATE 'SELECT THRESHHOLD_VALUE, REPORT_ID, TEST_SEQ,SESSION_NAME FROM '
|| v_test_suite_table
|| ' WHERE TEST_CASE_ID = :caseId'
INTO v_threshold_val,v_report_id,v_test_seq_no,v_session_name
USING p_test_case_id;
IF
v_amt_recs > v_threshold_val
THEN
v_test_result := 'PASS';
INSERT INTO BDW_AMPS.spares_bdw_test_results(
TEST_RESULT_ID,
BATCH_ID,
REPORT_ID,
TEST_CASE_ID,
TEST_SEQ_NO,
TABLE_NAME,
SESSION_NAME,
TEST_RESULT,
PROCESS_DATE,
PROCESS_BY
)
VALUES
( v_test_result_id ,
v_batch_id ,
v_report_id ,
p_test_case_id ,
v_test_seq_no ,
p_table_name ,
v_session_name ,
v_test_result ,
SYSDATE,
v_process_by
);
commit;
ELSE
v_test_result := 'FAIL';
v_error_description := 'Count: ' || v_amt_recs || ' is greater than threshold value: ' || v_threshold_val;
INSERT INTO BDW_AMPS.spares_bdw_test_results(
TEST_RESULT_ID,
BATCH_ID,
REPORT_ID,
TEST_CASE_ID,
TEST_SEQ_NO,
TABLE_NAME,
SESSION_NAME,
TEST_RESULT,
ERROR_DESCRIPTION,
PROCESS_DATE,
PROCESS_BY
)
VALUES (
v_test_result_id ,
v_batch_id ,
v_report_id ,
p_test_case_id ,
v_test_seq_no ,
p_table_name ,
v_session_name ,
v_test_result ,
v_error_description ,
SYSDATE,
v_process_by
);
commit;
END IF;
RETURN v_test_result;
END;
while usage of them are right for SELECT statements because of dynamic table names.

Gettig error PLS-00364

I'm trying to create a stored procedure where I'm passing select statement to for loop and i'm using dynamic table which is passing at runtime and getting below error:
LINE 23 PLS-00364: loop index variable 'I' use is invalid
LINE 19 PL/SQL: ORA-00942: table or view does not exist
CREATE OR REPLACE PROCEDURE CREATE_TEST(TBL_NM IN VARCHAR2)
IS
SRC_ID NUMBER(38);
SQL_Q VARCHAR2(250);
DEL_F VARCHAR2(250);
BEGIN
FOR I in (SELECT DEL_IND FROM TBL_NM)
LOOP
SRC_ID := SRC_FILE_ID_SEQ.NEXTVAL;
IF I.DEL_IND = 0
THEN
execute immediate 'INSERT INTO TEST_HIST ' || ' (a,b,c,d,e,DEL_IND) ' ||
' SELECT a,b,c,d,e, '|| 0 || ' || ' FROM ' || TBL_NM;
ELSIF I.DEL_IND = 1
THEN
execute immediate 'INSERT INTO JESTX_IGNR ' || ' (a,b,c,d,e,DEL_IND,SRC_ID_NO) ' ||
' SELECT a,b,c,d,e, '|| 2 ||' , '|| SRC_ID || ' FROM ' || TBL_NM;
END IF ;
END LOOP;
COMMIT;
END;
I call the procedure using:
EXEC CREATE_TEST('abc');
What you want is a REF CURSOR as you cannot use cursor for loop with dynamic sql.
I dont know the exact datatype for your column DEL_IND. Please declare accordingly.
Here is some more information on Oracle REF CURSORS.
Try below
CREATE OR REPLACE PROCEDURE CREATE_TEST(TBL_NM IN VARCHAR2)
IS
TYPE c1ref is REF CURSOR;
SRC_ID NUMBER(38);
SQL_Q VARCHAR2(250);
DEL_F VARCHAR2(250);
DEL_IND NUMBER(5);
BEGIN
vsql_text := 'select DEL_IND from ' || TBL_NM;
open c1ref for vsql_text;
LOOP
SRC_ID := SRC_FILE_ID_SEQ.NEXTVAL;
fetch c1ref into DEL_IND;
exit when c1ref%NOTFOUND;
IF (DEL_IND = 0)
THEN
execute immediate 'INSERT INTO TEST_HIST ' || ' (a,b,c,d,e,DEL_IND) ' ||
' SELECT a,b,c,d,e, '|| 0 || ' || FROM ' || TBL_NM;
ELSIF (DEL_IND = 1)
THEN
execute immediate 'INSERT INTO JESTX_IGNR ' || ' (a,b,c,d,e,DEL_IND) ' ||
' SELECT a,b,c,d,e, '|| 2 ||' , '|| SRC_ID || ' FROM ' || TBL_NM;
END IF ;
END LOOP;
CLOSE c1ref;
COMMIT;
END;

Spool the data from execute immediate within pl/sql block

Ho to spool the data from execute immediate in pl/sql.
Below is the code.
DECLARE
from_dt varchar(300):=&from_date;
to_dt varchar2(300):=&to_date;
account varchar2(200):=&account;
mbl_table varchar2(100);
dn_table varchar2(100);
id number;
Begin
select aid into id from account where upper(acode)=upper(account);
select tablename into mbl_table from partner_mbl where pid in (select pid from account where upper(acode)=upper(account));
select dn_tablename into dn_table from partner_mbl where pid in (select pid from account where upper(acode)=upper(account));
dbms_output.put_line(mbl_table);
FOR i IN (select 'p_'||to_char((ROWNUM-1 + to_date(from_dt, 'ddmonyyyy')),'ddmonyyyy') part FROM all_objects
WHERE ROWNUM-2 < to_date(to_dt, 'ddmonyyyy')-to_date(from_dt, 'ddmonyyyy'))loop
execute immediate 'Select a.pcode,A.Acode,To_Char(A.Mobile),a.msg,a.senderid,To_Char(A.Rts,'||'''dd/mm/yyyy hh24:mi:ss'''||'),To_Char(A.Sts,'||'''dd/mm/yyyy hh24:mi:ss'''||'),A.Statusflag,A.Statusid,To_Char(B.Lastts,'||'''dd/mm/yyyy hh24:mi:ss'''||'),B.Statusflag,B.Err_Des From '|| mbl_table ||' partition('||i.part||') A, '|| dn_table ||' partition('||i.part||') B Where a.aid='||id|| ' And A.Msgid = B.Msgid(+)';
end loop;
End;
how to spool the output of execute immediate into csv file
DECLARE
from_dt varchar(300) := &from_date;
to_dt varchar2(300) := &to_date;
account varchar2(200) := &account;
mbl_table varchar2(100);
dn_table varchar2(100);
id number;
/*define types*/
/* change the next varchar2(1000) with your types*/
t_pcode IS TABLE OF varchar2(1000);
v_pcode t_pcode;
t_Acode IS TABLE OF varchar2(1000);
v_Acode t_Acode;
t_Mobile IS TABLE OF varchar2(1000);
v_Mobile t_Mobile;
t_msg IS TABLE OF varchar2(1000);
v_msg t_msg;
t_senderid IS TABLE OF varchar2(1000);
v_senderid t_senderid;
t_Rts IS TABLE OF varchar2(1000);
v_Rts t_Rts;
t_Sts IS TABLE OF varchar2(1000);
V_Sts t_Sts;
t_Statusflag IS TABLE OF varchar2(1000);
v_Statusflag t_Statusflag;
t_Statusid IS TABLE OF varchar2(1000);
v_Statusid t_Statusid;
t_Lastts IS TABLE OF varchar2(1000);
v_Lastts t_Lastts;
t_Statusflag IS TABLE OF varchar2(1000);
v_Statusflag t_Statusflag;
t_Err_Des IS TABLE OF varchar2(1000);
v_Err_Des t_Err_Des;
/*end types*/
Begin
select aid into id from account where upper(acode) = upper(account);
select tablename
into mbl_table
from partner_mbl
where pid in
(select pid from account where upper(acode) = upper(account));
select dn_tablename
into dn_table
from partner_mbl
where pid in
(select pid from account where upper(acode) = upper(account));
dbms_output.put_line(mbl_table);
FOR i IN (select 'p_' ||
to_char((ROWNUM - 1 + to_date(from_dt, 'ddmonyyyy')),
'ddmonyyyy') part
FROM all_objects
WHERE ROWNUM - 2 < to_date(to_dt, 'ddmonyyyy') -
to_date(from_dt, 'ddmonyyyy')) loop
/* execute immediate 'Select a.pcode,A.Acode,To_Char(A.Mobile),a.msg,a.senderid,To_Char(A.Rts,' ||
'''dd/mm/yyyy hh24:mi:ss''' || '),To_Char(A.Sts,' ||
'''dd/mm/yyyy hh24:mi:ss''' ||
'),A.Statusflag,A.Statusid,To_Char(B.Lastts,' ||
'''dd/mm/yyyy hh24:mi:ss''' ||
'),B.Statusflag,B.Err_Des From ' || mbl_table ||
' partition(' || i.part || ') A, ' || dn_table ||
' partition(' || i.part || ') B Where a.aid=' || id ||
' And A.Msgid = B.Msgid(+)' ;
*/
execute immediate 'Select a.pcode,A.Acode,To_Char(A.Mobile),a.msg,a.senderid,To_Char(A.Rts,' ||
'''dd/mm/yyyy hh24:mi:ss''' || '),To_Char(A.Sts,' ||
'''dd/mm/yyyy hh24:mi:ss''' ||
'),A.Statusflag,A.Statusid,To_Char(B.Lastts,' ||
'''dd/mm/yyyy hh24:mi:ss''' ||
'),B.Statusflag,B.Err_Des From ' || mbl_table ||
' partition(' || i.part || ') A, ' || dn_table ||
' partition(' || i.part || ') B Where a.aid=' || id ||
' And A.Msgid = B.Msgid(+)'
BULK COLLECT INTO v_pcode, v_Acode, v_Mobile, v_msg, v_senderid,
v_Rts, V_Sts, v_Statusflag, v_Statusid, v_Lastts,
v_Statusflag, v_Err_Des;
for i.. v_pcode.count loop
dbms_output.put_line('pcode and msg values are: '||v_pcode(i)||' ' ||v_msg(i) ||' at line' || i );
/*you can get out other values with same way*/
end loop;
End;
you can also take a look at oracle documentation for "oracle collections", "execute immediate", "bulk collect".
my previous post could solve your problem however here is a simple example as additional info.
DECLARE
TYPE emp_typ IS TABLE OF scott.emp%ROWTYPE;
e_tab emp_typ;
BEGIN
EXECUTE IMMEDIATE 'SELECT * FROM emp'
BULK COLLECT INTO e_tab;
DBMS_OUTPUT.put_line('Dynamic EXECUTE: ' || e_tab.count);
for i in 1..e_tab.count loop
DBMS_OUTPUT.put_line( e_tab(i).ename ||':'|| e_tab(i).empno );
null;
end loop;
END;
/

Resources