Home » SQL & PL/SQL » SQL & PL/SQL » dynamic cursor (oracle 11.2.0.3 sun solaris 10.5)
dynamic cursor [message #598189] Fri, 11 October 2013 05:39 Go to next message
guddu_12
Messages: 227
Registered: April 2012
Location: UK
Senior Member
Dear all,

I have a plsql block construct where i want to use for loop dynamically , the query which for cursor for for loop will accept the table name from parameter and join them to return the result. the resultant data will iterate in loop and do the execution.

I am unable to achive it, can you please give me a clue


DECLARE
--initialize variables here 
    v_date varchar2(10); 
    v_rebuild_index   varchar2(250); 
    v_sql   VARCHAR2(250);
    p_table_name varchar2(250) := 'DS_ABSENCE';
    p_source_table varchar2(30) := 'STG_ABSENCE';
    p_source_owner varchar2(30)  := 'STG_SAP';
    v_for_sql varchar2(1000);
-- main body 
BEGIN 
       
     v_for_sql := 'select TO_CHAR(MIN(a.calendar_date),''YYYY-MM-DD'') start_date_of_period 
                            FROM ' || p_source_owner||'.'||p_source_table ||' a, conformed_dimension.dim_calendar b 
                            WHERE a.calendar_date = b.calendar_date 
                            GROUP BY b.start_date_of_period';
        
                     
     FOR cur_period IN (v_for_sql) LOOP 
         
        v_date := cur_period.start_date_of_period; 
        
        v_sql := 'DELETE FROM '|| p_table_name || ' WHERE calendar_date = ' ||v_date; 
                  
         dbms_output.put_line (v_sql); 
                
        --EXECUTE IMMEDIATE v_sql; 

        END LOOP;  

END;
/
Re: dynamic cursor [message #598190 is a reply to message #598189] Fri, 11 October 2013 05:46 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
Well, the quick answer is don't do this. This can be achieved in a single statement.
Re: dynamic cursor [message #598191 is a reply to message #598189] Fri, 11 October 2013 05:46 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
You can't use for loops with dynamic sql.
You need use a manual loop with a ref cursor, or use dbms_sql. Or bulk collect the dynamic query into an array and loop over that.
But I see no need for a loop here, you can just write a single dynamic delete.
icon12.gif  Re: dynamic cursor [message #598208 is a reply to message #598189] Fri, 11 October 2013 08:50 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

2 good books for you:
PL/SQL User's Guide and Reference
Application Developer's Guide - Fundamentals

Re: dynamic cursor [message #598314 is a reply to message #598208] Sun, 13 October 2013 05:22 Go to previous messageGo to next message
guddu_12
Messages: 227
Registered: April 2012
Location: UK
Senior Member
THanks michal,

Really nice book
Re: dynamic cursor [message #598319 is a reply to message #598314] Sun, 13 October 2013 06:14 Go to previous messageGo to next message
guddu_12
Messages: 227
Registered: April 2012
Location: UK
Senior Member
Can you please provide me link for 11gr2 for the above book
icon1.gif  Re: dynamic cursor [message #598320 is a reply to message #598319] Sun, 13 October 2013 06:20 Go to previous message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

http://www.oracle.com/pls/db112/homepage
Re: dynamic cursor [message #598321 is a reply to message #598319] Sun, 13 October 2013 06:20 Go to previous message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/toc.htm
http://docs.oracle.com/cd/E11882_01/appdev.112/e41502/toc.htm

Just google search Smile
Previous Topic: Why the second query is more efficient?
Next Topic: calling a function in another function to insert a dml record is failing
Goto Forum:
  


Current Time: Tue Apr 23 09:57:30 CDT 2024