Site Network: Home | About

I got the following from "I am feeling Lucky" for "Difference between Regular Cursors and Ref Cursors" search string :)). Please don't think like "wht is the necessity in having a post on this, if it is already the first hit from Google". A big NO. It's extraction from several such search good hits. Once again, thanks to Google, making lives easy. ok, let us go down. These are Toms words in asktom... Technically, under the covers, at the most "basic level", they are the same. A "normal" plsql cursor is static in defintion. Ref cursors may be dynamically opened or opened based on logic. Look at the following code Declare type rc is ref cursor; cursor c is select * from dual; l_cursor rc; -- Referential cursor declaration begin if ( to_char(sysdate,'dd') = 30 ) then open l_cursor for 'select * from emp'; -- l_cursor can be for #select * from emp# elsif ( to_char(sysdate,'dd') = 29 ) then open l_cursor for select * from dept; -- l_cursor can be for #select * from dept# else open l_cursor for select * from dual; -- l_cursor can even be for #select * from dual# end if; open c; -- but cursor C is always for # select * from dual # end; / Given that block of code -- you see perhaps the most "salient" difference -- no matter how many times you run that block -- cursor C will always be select * from dual. The ref cursor can be anything. Another difference is, a ref cursor can be returned to a client. a plsql "cursor cursor" cannot be returned to a client. -- We are using this feature of Ref cursor in our project. We r using Pro C as a client to Oracle DB. From Pro C, we call the stored procedure which would return a ref cursor to the calling environment. Another difference is, a cursor can be global -- a ref cursor cannot (you cannot define them OUTSIDE of a procedure / function) Another difference is, a ref cursor can be passed from subroutine to subroutine, a cursor cannot be. -- U can find an example on this if u scroll down Another difference is that static sql (not using a ref cursor) is much more efficient than using ref cursors and that use of ref cursors should be limited to - returning result sets to clients - when there is NO other efficient/effective means of achieving the goal that is, you want to use static SQL (with implicit cursors really) first and use a ref cursor only when you absolutely have to. Ex: A ref cursor, passed as a parameter... CREATE OR REPLACE PROCEDURE pass_ref_cur(p_cursor SYS_REFCURSOR) IS TYPE array_t IS TABLE OF VARCHAR2(4000) INDEX BY BINARY_INTEGER; rec_array array_t; BEGIN FETCH p_cursor BULK COLLECT INTO rec_array; FOR i IN rec_array.FIRST .. rec_array.LAST LOOP dbms_output.put_line(rec_array(i)); END LOOP; END pass_ref_cur; / DECLARE rec_array SYS_REFCURSOR; BEGIN OPEN rec_array FOR 'SELECT empname FROM employees'; pass_ref_cur(rec_array); CLOSE rec_array; END; / Here Ref Cursor is weakly typed. For strongly typed declaration we can use TYPE return_cur IS REF CURSOR RETURN all_tables%ROWTYPE; Hold on a moment, wht is the diff b/w Strong and Weakly typed cursor?. Structure of the ref cursor is known by the compiler at compile time. create or replace package my_pkg as type strong is ref cursor return emp%rowtype; type weak is ref cursor; end; / Package created. STRONG's structure -- the columns, their datatypes, their lengths, everything -- are well known at compile time. It can only be used to return rows that look exactly like the EMP table (doesn't have to be from the EMP table -- just must be the same STRUCTURE). WEAK's structure -- not known. It could be used with: open a_weak_one for select * from dual; or open a_weak_one for select * from dept; Its structure is NOT known at compile time and may actually change from use to use. Here I have question for u.... There will not be a significant performance difference between Strong and Weakly typed ref cursors. Then why should I go for Strongly typed cursors. Think again, before scrolling down further. Why should one declare a cursor variable in a complex way, mentioning the return type also?. Strongly typed ones can be "described". These are "easier for a tool like forms or powerbuilder to layout a screen to hold the results". I should have posted this Stong and Weak ref cursors in a seperate thread. I will do it, once I get more information on these. Catch u here again -- Karteeek

0 Comments:

Post a Comment