In previous post we discussed about implicit & explicit cursor and also understood how it is different from cursor variable.Lets reiterate it again, both explicit cursor and implicit cursor are associated with a specific SQL DML statement(they are tied to specific queries), whereas cursor variable can refer to multiple DML statements (off course SELECT statement) throughout session. Read The main agenda of this post is to understand the declaration and uses of cursor variable.
Note:- Once cursor is closed, if try to fetch rows it will throws error ORA-01001: invalid cursor.
Note:- When a cursor variable is created in PL/SQL program unit, it is just a reference variable. Actual cursor object (the result set identified by the cursor SQL statement) is created when OPEN FOR is executed for select query and assigned to a cursor variable. See the following diagram to understand difference between cursor variable and cursor object:-
Lets write a sample program to understand how do we create concrete cursor type and its variable to fetch records:-
==========Sample output========================
--First binding output--
FIRST_NAME EMAIL
------------- ---------------
Steven
SKING@devinline.com
Neena
NKOCHHAR@devinline.com
Lex
LDEHAAN@devinline.com
NIKHIL
NIKSR@devinline.com
--Second binding output --
DEPARTMNET_NAME MANAGER_ID
------------- ---------------
Marketing 201
Purchasing 114
========================================
In above program, cursor type is created followed by a cursor variable and two variable one of type employee row and another of department row type is created. In begin block, cursor is opened and associated it with a select statement. Fetch cursor in Loop and display name and email. Again same cursor is bind with another select statement and again fetch and display rows from it. Above program unit uses same cursor variable to refer two select statement one after another, it is most important and noticeable feature of cursor variable.
Question:- Why error "ORA-01001: invalid cursor " is thrown, if try to fetch rows after cursor has been closed ?
Answer:- Always remember, cursor does nor contain value, it is just a pointer to result set. Once we execute close command, pointer to result set is removed so , cursor is just a variable now and no cursor object is attached to it, so it throws error.
Note:-
1. Difference between strong cursor type and weak cursor type
2. Difference between SYS_REFCURSOR and REF CURSOR
3. Difference between explicit cursor and cursor variable
4. REF CURSOR, SQL and PL/SQL interaction
Previous: PL/SQL Implicit & Explicit Cursor Next: PL/SQL program unit- Procedure and Function
Cursor variable -- a reference to a cursor object or query in the database.
Cursor variable is a pointer/reference to SQL work area and its value is the address of the that work area.Since it has address of SQL work area, it can accommodate multiple select statements(as shown here) returning different result sets(rows from different table).Syntax and life cycle of cursor variable:-
Cursor variable is of type REF CURSOR/ SYS_REFCURSOR. General syntax of cursor type & variable declaration and life cycle of cursor variable is as is as follows: first create type of cursor then create variable of that type termed as cursor variable followed by FETCH and close.Note:- Once cursor is closed, if try to fetch rows it will throws error ORA-01001: invalid cursor.
/* Create the cursor type.- Return type is optional */
TYPE cur_type IS REF CURSOR [RETURN table%ROWTYPE];
/* Declare a cursor variable of that type. */
cur_var cur_type;
/* Open the cursor variable, associating with it a SQL statement. */
OPEN cur_type FOR <SELECT statement>;
/* Fetch from the cursor variable. */
FETCH cur_var INTO <table_rec_type/use%ROWTYPE>;
/* Close the cursor object associated with variable. */
CLOSE cur_var;
Cursor variable and Cursor object :-
Cursor variable dynamically binds with select statement when OPEN FOR syntax is executedNote:- When a cursor variable is created in PL/SQL program unit, it is just a reference variable. Actual cursor object (the result set identified by the cursor SQL statement) is created when OPEN FOR is executed for select query and assigned to a cursor variable. See the following diagram to understand difference between cursor variable and cursor object:-
Cursor variable and cursor Object creation in PL/SQL- cursor variable is reference to cursor Object |
DECLARE /*Cursor type declaration*/ TYPE v_empcur_type IS REF CURSOR;-- RETURN EMPLOYEES%ROWTYPE; /*cursor variable declaration - no select statement binding here*/ cur_var v_empcur_type; v_deptId NUMBER(5) := 90; v_locId NUMBER(5) := 1800; v_emp_row EMPLOYEES%ROWTYPE; v_dept_row DEPARTMENTS%ROWTYPE; BEGIN /*First binding:- OPEN refcursor for a select statement */ OPEN cur_var FOR select * from employees where DEPARTMENT_ID = v_deptId; dbms_output.put_line('--First binding output--'); dbms_output.put_line('FIRST_NAME' || ' '|| 'EMAIL'); dbms_output.put_line('------------- ---------------'); LOOP FETCH cur_var INTO v_emp_row; EXIT WHEN cur_var%NOTFOUND; dbms_output.put_line(v_emp_row.FIRST_NAME || ' ' || v_emp_row.EMAIL||'@devinline.com'); END LOOP; /*Second binding :- OPEN refcursor for another select statement */ OPEN cur_var FOR select * from DEPARTMENTS where LOCATION_ID = v_locId; dbms_output.put_line('--Second binding output --'); dbms_output.put_line('DEPARTMNET_NAME' || ' '|| 'MANAGER_ID'); dbms_output.put_line('------------- ---------------'); LOOP FETCH cur_var INTO v_dept_row; EXIT WHEN cur_var%NOTFOUND; dbms_output.put_line(v_dept_row.DEPARTMENT_NAME || ' ' || v_dept_row.MANAGER_ID); END LOOP; CLOSE cur_var; END;
--First binding output--
FIRST_NAME EMAIL
------------- ---------------
Steven
SKING@devinline.com
Neena
NKOCHHAR@devinline.com
Lex
LDEHAAN@devinline.com
NIKHIL
NIKSR@devinline.com
--Second binding output --
DEPARTMNET_NAME MANAGER_ID
------------- ---------------
Marketing 201
Purchasing 114
========================================
In above program, cursor type is created followed by a cursor variable and two variable one of type employee row and another of department row type is created. In begin block, cursor is opened and associated it with a select statement. Fetch cursor in Loop and display name and email. Again same cursor is bind with another select statement and again fetch and display rows from it. Above program unit uses same cursor variable to refer two select statement one after another, it is most important and noticeable feature of cursor variable.
Question:- Why error "ORA-01001: invalid cursor " is thrown, if try to fetch rows after cursor has been closed ?
Answer:- Always remember, cursor does nor contain value, it is just a pointer to result set. Once we execute close command, pointer to result set is removed so , cursor is just a variable now and no cursor object is attached to it, so it throws error.
Note:-
- Cursor variable also has same set of attributes as explicit cursor : ISOOPEN, FOUND, NOTFOUND, ROWCOUNT. Refer this table for more detail about cursor attributes.
- Cursor variable can be of two types: Strong type and Weak type.If RETURN clause is added while creating cursor type it is termed as Strong type, however if is missing then that type is called Weak type.Read Difference between strong type and weak type. in more detail.
- We can perform assignment operations with cursor variables and also pass these variables as arguments to procedures and functions. If either of cursor variable participating in assignment operation are weak then compile time check cannot be achieved. And if there is a type mismatch or both cursor variable are not structurally same then error is reported at runtime.
If both cursor variable is strong type, compile time check is done and error is reported if both are structurally not same.--Both cursor variable are strong type,compiler time check for structure compitablity Type emp_cur_type IS REF CURSOR RETURN employees%ROWTYPE; Type dept_cur_type IS REF CURSOR RETURN departments%ROWTYPE; emp_cur_var emp_cur_type; dept_cur_var dept_cur_type; ..... /*compile time error for below assignment: PLS-00382: expression is of wrong type*/ emp_cur_var := dept_cur_var;
- A cursor object(a query/result-set) may be referenced by two cursor variable.
- NULL cannot be assigned to a cursor variable.
- Cursor variables cannot be declared in a package since they do not have a persistent state.
1. Difference between strong cursor type and weak cursor type
2. Difference between SYS_REFCURSOR and REF CURSOR
3. Difference between explicit cursor and cursor variable
4. REF CURSOR, SQL and PL/SQL interaction
Previous: PL/SQL Implicit & Explicit Cursor Next: PL/SQL program unit- Procedure and Function
This blog was making more interesting. Keep adding more information on your page.
ReplyDeleteC C++ Training in Chennai
C Training in Chennai
C++ Training in Chennai
JMeter Training in Chennai
JMeter Training Institute in Chennai
Appium Training in Chennai
javascript training in chennai
core java training in chennai
I would definitely say that this blog is really useful for me and helped me to gain a clear basic knowledge on the topic. Waiting for more updates from this blog admin.
ReplyDeleteIELTS Classes in Mumbai
IELTS Coaching in Mumbai
IELTS Mumbai
IELTS Center in Mumbai
Best IELTS Coaching in Mumbai
Spoken English Classes in Chennai
IELTS Coaching in Chennai
English Speaking Classes in Mumbai
Awesome blog with great piece of information. Very well written blog with crisp and neat content. Keep sharing more such blogs.
ReplyDeleteCloud Computing Training in Chennai
Cloud Training in Chennai
Data Science Course in Chennai
Azure courses in Chennai
VMware course
R Programming Training in Chennai
Cloud Certification in Chennai
Nice blog! Thanks for sharing this valuable information
ReplyDeleteGerman Classes in Chennai
German Classes in Bangalore
German Classes in Coimbatore
German Classes in Madurai
German Language Course in Hyderabad
German Courses in Chennai
German Courses in Bangalore
German Courses in Coimbatore
German classes in marathahalli
Tally Course in Coimbatore
This blog is really awesome. I learned lots of informations in your blog. Keep posting like this...
ReplyDeleteSelenium Training in Chennai
Selenium Training in Bangalore
Selenium Training in Coimbatore
Best Selenium Training in Bangalore
Selenium Course in Bangalore
Selenium Training Institute in Bangalore
selenium training in marathahalli
Software Testing Course in Chennai
Hacking Course in Bangalore
That's a beautiful post. I can't wait to utilize the resources you've shared with us. Do share more such informative posts.
ReplyDeleteData Science Course in Chennai
Data Science Classes in Chennai
R Training in Chennai
AWS Training in Chennai
Data Science Training in Guindy
Data Science Training in Thiruvanmiyur
Data Science Training in Anna Nagar
Pretty blog, so many ideas in a single site, thanks for the informative article, keep updating more article.
ReplyDeleteDigital Marketing Training in Bangalore
digital marketing training in marathahalli
Digital Marketing Training in Coimbatore
Digital Marketing Course in Madurai
digital marketing courses in btm
Excellent blog, keep sharing this blog. This blog contains full of usefull information..
ReplyDeleteDOT NET Training in Chennai
DOT NET Training in Bangalore
DOT NET Training Institutes in Bangalore
DOT NET Course in Bangalore
Best DOT NET Training Institutes in Bangalore
DOT NET Institute in Bangalore
DOT NET Training Institute in Marathahalli
PHP Training in Bangalore
Spoken English Classes in Bangalore
Data Science Courses in Bangalore
Such a great blog.Thanks for sharing.........
ReplyDeleteEthical Hacking Course in Chennai
Ethical hacking course in bangalore
Ethical hacking course in coimbatore
Ethical Hacking Training in Bangalore
Certified Ethical Hacking Course in Chennai
Ethical Hacking in Bangalore
Hacking Course in Bangalore
Ethical Hacking institute in Bangalore
Selenium Training in Bangalore
Software Testing course in Bangalore
aws interview questions and answers for experienced
ReplyDeleteAWS Interview Questions and Answers for freshers and experienced to get your dream job in AWS! 101 AWS Interview Questions for Freshers, aws interview questions and answers for experienced
Amazing post to keep updating more information.
ReplyDeletestatistics tutorial for data science
ethical hacking tutorials
Mua vé tại đại lý vé máy bay Aivivu, tham khảo
ReplyDeleteVe may bay di My
máy bay đà lạt hà nội
giá vé máy bay hà nội hồ chí minh
giá vé máy bay tphcm đi nha trang
vé máy bay eva từ mỹ về việt nam
taxi sân bay nội bài
it was so good to read and useful
ReplyDeleteGerman Classes in Tambaram
German Classes in Anna Nagar
German Classes in Velachery
German Classes in T Nagar
German Classes in Porur
German Classes in OMR
German Classes in chennai
Useful Information..!!! Best blog with effective information’s..!!
ReplyDeleteJava Training Institute in Chennai
Selenium Training Institute in Chennai
Python Classes in Chennai
AWS Certification in Chennai
Data Science Certification in Chennai
DevOps course in Chennai
Great Blog!!! Was an interesting blog with a clear concept. And will surely help many to update them.
ReplyDeleteBig Data Hadoop Training Institute
Big Data Training Institute
AngularJS TrainingSAS Training in Chennai
Great experience for me by reading this blog. Thank you for the wonderful article.
ReplyDeleteRPA Training in Tambaram
RPA Training in Anna Nagar
RPA Training in Velachery
RPA Training in T nagar
RPA training in Porur
RPA Training in OMR
RPA Training in Chennai
Great post. keep sharing such a worthy information
ReplyDeletePHP Course in Chennai
PHP Course in Bangalore
Informative blog... Thanks for sharing and keep updating
ReplyDeleteEthical Hacking Course in Chennai
Ethical Hacking course in Bangalore
ReplyDeleteFabulous Blog...good informative keep sharing
Digital Marketing Course in Chennai
Digital Marketing Training in Chennai
Digital Marketing Institute in Bangalore
Best Digital Marketing Courses in Bangalore
Digital Marketing Training Institute in Bangalore
Great post. keep sharing such a worthy information
ReplyDeleteDigital Marketing Training in Chennai
Digital marketing online course
Great post. keep sharing such a worthy information
ReplyDeleteDevOps course in Chennai
DevOps Course in Bangalore
Great share!
ReplyDeleteRPA Training Institute in Chennai
Rpa training in bangalore
Good Blog.. keep more updates
ReplyDeleteData Science Course in Chennai
Data Science Training in Chennai
Data Science Certification in Chennai
Data Science Courses in Bangalore
Data Science Training in Bangalore
Excellent post, it will be definitely helpful for many people. Keep posting more like this.
ReplyDeleteaws interview questions and answers pdf
aws interview questions and answers for freshers
aws interview questions and answers for freshers pdf
instagram takipçi satın al - instagram takipçi satın al - takipçi satın al - takipçi satın al - instagram takipçi satın al - takipçi satın al - instagram takipçi satın al - aşk kitapları - tiktok takipçi satın al - instagram beğeni satın al - youtube abone satın al - twitter takipçi satın al - tiktok beğeni satın al - tiktok izlenme satın al - twitter takipçi satın al - tiktok takipçi satın al - youtube abone satın al - tiktok beğeni satın al - instagram beğeni satın al - trend topic satın al - trend topic satın al - youtube abone satın al - beğeni satın al - tiktok izlenme satın al - sms onay - youtube izlenme satın al - tiktok beğeni satın al - sms onay - sms onay - perde modelleri - instagram takipçi satın al - takipçi satın al - tiktok jeton hilesi - pubg uc satın al - sultanbet - marsbahis - betboo - betboo - betboo
ReplyDeleteUcuz, kaliteli ve organik sosyal medya hizmetleri satın almak için Ravje Medyayı tercih edebilir ve sosyal medya hesaplarını hızla büyütebilirsin. Ravje Medya ile sosyal medya hesaplarını organik ve gerçek kişiler ile geliştirebilir, kişisel ya da ticari hesapların için Ravje Medyayı tercih edebilirsin. Ravje Medya internet sitesine giriş yapmak için hemen tıkla: https://www.ravje.com
ReplyDeleteİnstagram takipçi satın almak için Ravje Medya hizmetlerini tercih edebilir, güvenilir ve gerçek takipçilere Ravje Medya ile ulaşabilirsin. İnstagram takipçi satın almak artık Ravje Medya ile oldukça güvenilir. Hemen instagram takipçi satın almak için Ravje Medyanın ilgili sayfasını ziyaret et: instagram takipçi satın al
Tiktok takipçi satın al istiyorsan tercihini Ravje Medya yap! Ravje Medya uzman kadrosu ve profesyonel ekibi ile sizlere Tiktok takipçi satın alma hizmetide sunmaktadır. Tiktok takipçi satın almak için hemen tıkla: tiktok takipçi satın al
Great blog.thanks for sharing such a useful information
ReplyDeleteBig Data Hadoop Training
Excellent information blog .Thanks for sharing.
ReplyDeleteAndroid Training in Chennai
Android Course Online
Android Training in Bangalore
Happy to read the informative blog. Thanks for sharing
ReplyDeletepython training institute in chennai
python institute in chennai
This post is so interactive and informative.keep update more information...
ReplyDeletedot net training in anna nagar
Dot net training in Chennai
Thanks for sharing this blog. It was so informative.
ReplyDeleteWhere do you see yourself after 5 years
Today question
This post is so interactive and informative.keep update more information...
ReplyDeletePHP Training in Anna nagar
PHP Training in Anna nagar
ReplyDeletehttps://www.newdaypuppies.com/
https://www.newdaypuppies.com/teacup-yorkie-puppies-for-sale/
https://www.newdaypuppies.com/tea-cup-yorkie-puppy-for-sale/
https://www.newdaypuppies.com/yorkshire-terrier-for-sale-near-me/
https://www.newdaypuppies.com/yorkie-terrier-puppy-for-sale-near-me/
This post is so interactive and informative.keep update more information...
ReplyDeleteJava Training in Tambaram
java course in tambaram
youtube link to mp3 songs
ReplyDeletevimeo youtube to mp3
youtube video converter to mp3 iphone
which youtube to mp3 converter is safe
simple youtube to mp3/mp4 converter firefox
https://yttomp3.pro/
websites for converting youtube videos to mp3 free
youtube to mp3 converter online free fast
free youtube to mp3 conversor ogg
youtube to mp3 downloader.
nevşehir
ReplyDeletearnavutköy
bandırma
fethiye
kars
RNMO4H
yurtdışı kargo
ReplyDeleteresimli magnet
instagram takipçi satın al
yurtdışı kargo
sms onay
dijital kartvizit
dijital kartvizit
https://nobetci-eczane.org/
V4V
salt likit
ReplyDeletesalt likit
dr mood likit
big boss likit
dl likit
dark likit
4UP8LR
salt likit
ReplyDeletesalt likit
RFVZX
excellent blog.that was very well explained.
ReplyDeleteSQL Classes in Pune
Great experience I got good information from your blog. Ziyyara’s expert phonics tutors, available for personalized one-to-one sessions, create a harmonious learning experience that tunes into your child's unique needs.
ReplyDeletevisit phonics home tutor