الانتقال إلى المحتوى

ما المقصود بــ REF CURSOR ?


m_nasef

Recommended Posts

هلا اخي
هذا الرد من موقع توك كيت عن الفرق بينه وبين الكيرسور العادي

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.

Declare
  type rc is ref cursor;
  
  cursor c is select * from dual;

  l_cursor rc;
begin
  if ( to_char(sysdate,'dd') = 30 ) then
    open l_cursor for 'select * from emp';
  elsif ( to_char(sysdate,'dd') = 29 ) then
    open l_cursor for select * from dept;
  else
    open l_cursor for select * from dual;
  end if;
  open c;
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.

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.

Another difference is that static sql (not using a ref cursor) is much more 
efficient then 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

Then sit back and say "anything else you wanted to know about them" 


في النهاية نستطيع ان نقول ان الكيرسور العادي تعريفه ثابت اما الاخر فيمكن ان يتغير او يكون dynamic

مع تمنياتي بالتوفيق

رابط هذا التعليق
شارك

A cursor variable may be used to point to the area in memory where the result of a multirow query is stored. The cursor always refers to the same information in a work area, while a cursor variable can point to different work areas.

Example
----------------------------------

TYPE inst_city_type IS RECORD
(first_name instructor.first_name%TYPE;
last_name  instructor.last_name%TYPE;
city       zipcode.city%TYPE;
state      zipcode.state%TYPE)

TYPE ref_type_name is REF CURSOR [RETURN return_type];

TYPE inst_city_cur IS REF CURSOR RETURN inst_city_type;

a Complete Example
-------------------------------------------
CREATE OR REPLACE PACKAGE course_pkg AS TYPE course_rec_typ IS RECORD
   (first_name    student.first_name%TYPE,
    last_name     student.last_name%TYPE,
    course_no     course.course_no%TYPE,
    description   course.description%TYPE,
    section_no    section.section_no%TYPE
    );
 TYPE course_cur IS REF CURSOR RETURN course_rec_typ;
 PROCEDURE get_course_list
   (p_student_id    NUMBER ,
    p_instructor_id NUMBER ,
    course_list_cv IN OUT course_cur);
END course_pkg;

CREATE OR REPLACE PACKAGE BODY course_pkg AS PROCEDURE get_course_list
   (p_student_id    NUMBER ,
    p_instructor_id NUMBER ,
    course_list_cv IN OUT course_cur)
 IS
 BEGIN IF p_student_id IS NULL AND p_instructor_id
     IS NULL THEN
     OPEN course_list_cv FOR
       SELECT 'Please choose a student-' First_name,
              'instructor combination'   Last_name,
         NULL     course_no,
         NULL     description,
         NULL     section_no
         FROM dual; ELSIF p_student_id IS NULL  THEN
     OPEN course_list_cv FOR
       SELECT s.first_name    first_name,
         s.last_name     last_name,
         c.course_no     course_no,
         c.description   description,
         se.section_no   section_no
  FROM instructor i, student s,
         section se, course c, enrollment e
  WHERE i.instructor_id = p_instructor_id AND i.instructor_id = se.instructor_id AND se.course_no    = c.course_no AND e.student_id    = s.student_id AND e.section_id    = se.section_id
    ORDER BY c.course_no, se.section_no;
   ELSIF p_instructor_id IS NULL  THEN
     OPEN course_list_cv FOR
          SELECT i.first_name    first_name,
         i.last_name     last_name,
         c.course_no     course_no,
         c.description   description,
         se.section_no   section_no
  FROM instructor i, student s,
         section se, course c, enrollment e
  WHERE s.student_id = p_student_id AND i.instructor_id = se.instructor_id AND se.course_no    = c.course_no AND e.student_id    = s.student_id AND e.section_id    = se.section_id
       ORDER BY c.course_no, se.section_no;
       END IF;
    END get_course_list;

END course_pkg;



Oracle ITself

رابط هذا التعليق
شارك

  • بعد 3 سنة...

السلام عليكم انا عندي شغله اريد اسويها وهي انه عندي جداول تحتوي على Attributes وبالتاكيد توجد بيانات في الجداول بحيث جدول رئيسي والباقي متفرعه منه مرتبطة معه عن طريق Foriegn key السؤال؟ اريد عمل برنامج فقط لاستخراج البيانات بحيث يقوم بعمل Check في الجدول الرئيسي لقيمه ضمن Attribute معين واذا وجدها يذهب عن طريقها الى الجدول الفرعي والذي يحتوي على التفاصيل.

بمعنى اخر عندي جدول رئيسي والاخر فرعي وبدي اتجه من الرئيسي الى الفرعي dynamically حسب مافهمت انه تتم هذه العملية عن طريق الـ reference cursor ارجو المساعده باي شكل رجاءأ حتى لو يوجد شرح مفصل الى الـ Ref Cursor وانا ادرسة حتى لو باللغه الانكليزية ولكم مني فائق الاحترام.

(مستعجل جداً)

تم تعديل بواسطة anmarthehip
رابط هذا التعليق
شارك

  • بعد 2 سنة...

السلام عليكم انا عندي شغله اريد اسويها وهي انه عندي جداول تحتوي على Attributes وبالتاكيد توجد بيانات في الجداول بحيث جدول رئيسي والباقي متفرعه منه مرتبطة معه عن طريق Foriegn key السؤال؟ اريد عمل برنامج فقط لاستخراج البيانات بحيث يقوم بعمل Check في الجدول الرئيسي لقيمه ضمن Attribute معين واذا وجدها يذهب عن طريقها الى الجدول الفرعي والذي يحتوي على التفاصيل.

بمعنى اخر عندي جدول رئيسي والاخر فرعي وبدي اتجه من الرئيسي الى الفرعي dynamically حسب مافهمت انه تتم هذه العملية عن طريق الـ reference cursor ارجو المساعده باي شكل رجاءأ حتى لو يوجد شرح مفصل الى الـ Ref Cursor وانا ادرسة حتى لو باللغه الانكليزية ولكم مني فائق الاحترام.

(مستعجل جداً)




اخي الكريم / انا عارف ان الرد جاء بعد فترة كبيرة ولكن انا الان كنت ابحث مثلك عن نفس المشكلة الى ان هداني الله الى هذا الكتاب ..
كتاب قيم جدا جدا في الشرح مع كثير من الامثلة وبه ما تريد
اسمة : Oracle PL/SQL by Example
لينك التحميل : http://www.4shared.c...4thEdition.html

ادخل وابحث بداخلة عن ما تريد . تم تعديل بواسطة 3mr 3bdeen
رابط هذا التعليق
شارك

انضم إلى المناقشة

يمكنك المشاركة الآن والتسجيل لاحقاً. إذا كان لديك حساب, سجل دخولك الآن لتقوم بالمشاركة من خلال حسابك.

زائر
أضف رد على هذا الموضوع...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   تمت استعادة المحتوى السابق الخاص بك.   مسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

جاري التحميل
×
×
  • أضف...

برجاء الإنتباه

بإستخدامك للموقع فأنت تتعهد بالموافقة على هذه البنود: سياسة الخصوصية