Cursor Attributes: %FOUND, %NOTFOUND, %ISOPEN, %ROWCOUNT

Cursor attributes built-in database dashboard metrics के रूप में काम करते हैं जो आपके programs को एक query execution की बिल्कुल live स्थिति बताते हैं।

10 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

अपनी query की धड़कन जाँचना

कल्पना कीजिए आप TicketDesk से उन rows को fetch करने के लिए एक loop चलाते हैं जिन्हें तत्काल ध्यान चाहिए। आपका PL/SQL program असल में कैसे जानता है कि अंतिम ticket खींच लिया गया? क्या होता है अगर आप एक ऐसे cursor से fetch करने की कोशिश करते हैं जिसे आप open करना भूल गए, या आप अब तक process किए records की कुल संख्या कैसे दिखाते हैं? आप अपनी नंगी आँखों से database memory के अंदर नहीं देख सकते। आपको built-in indicators का एक set चाहिए जो किसी भी दिए millisecond पर आपके cursor की स्थिति तुरंत उजागर करे।

Theory

Car Dashboard Indicators

एक cursor को एक delivery van चलाने की तरह सोचिए। आप चलते समय engine या fuel tank के अंदर सीधे नहीं देख सकते। इसके बजाय, आप अपने dashboard को देखते हैं। fuel gauge आपको बताता है कि petrol बचा है या नहीं (%FOUND या %NOTFOUND)। odometer आपको बिल्कुल बताता है कि आपने कितने किलोमीटर तय किए (%ROWCOUNT)। ignition light आपको बताती है कि engine चल रहा है या पूरी तरह बंद है (%ISOPEN)। ये dashboard metrics आपको वाहन को अलग किए बिना live feedback देते हैं।

Theory

Oracle PL/SQL में Cursor Attributes

Oracle database dialect में, cursor attributes system-परिभाषित properties हैं जो एक cursor के लिए status flags के रूप में काम करते हैं। हर बार जब आप एक SQL statement execute करते या एक cursor operation करते हैं, Oracle अपने-आप private memory क्षेत्र में इन attributes को update करता है। percentage symbol (%) इस्तेमाल करके अपने cursor variable में एक attribute name जोड़कर, आपका procedural code row counts और fetch की सफलता जैसी runtime properties जाँच सकता है ताकि dynamic execution फ़ैसले ले।

At a glance

Oracle PL/SQL programming engine में मूल cursor status attributes

AttributeTypeReturn Value Description
%FOUNDBOOLEANTRUE return करता है अगर पिछले FETCH ने एक row लौटाई: विफल होने पर FALSE
%NOTFOUNDBOOLEANTRUE return करता है अगर पिछला FETCH एक row खोजने में विफल रहा: सफल होने पर FALSE
%ISOPENBOOLEANTRUE return करता है अगर cursor मौजूदा में open और memory में सक्रिय है: बंद होने पर FALSE
%ROWCOUNTNUMBERखुलने के बाद से अब तक fetch या प्रभावित rows की कुल संख्या return करता है

Practical

Attributes के साथ Ticket Audits track करना

-- Enable console output in Oracle environment
SET SERVEROUTPUT ON;

DECLARE
  CURSOR c_high_priority IS
    SELECT id, title FROM tickets WHERE priority = 'High';
    
  v_id tickets.id%TYPE;
  v_title tickets.title%TYPE;
BEGIN
  -- Check if cursor is already active before opening
  IF NOT c_high_priority%ISOPEN THEN
    OPEN c_high_priority;
  END IF;
  
  LOOP
    FETCH c_high_priority INTO v_id, v_title;
    
    -- Stop loop automatically when no rows remain
    EXIT WHEN c_high_priority%NOTFOUND;
    
    DBMS_OUTPUT.PUT_LINE('Fetched row number ' || c_high_priority%ROWCOUNT || ': ' || v_title);
  END LOOP;
  
  DBMS_OUTPUT.PUT_LINE('Total tickets processed: ' || c_high_priority%ROWCOUNT);
  CLOSE c_high_priority;
END;
/

Copy करके Oracle FreeSQL खोलें
Oracle FreeSQL, Oracle SQL का मुफ़्त online editor है। Code पहले copy हो जाता है: उसे वहां paste करके Run करें।

Quiz

मान लीजिए एक explicit cursor ने एक loop execution के दौरान सफलतापूर्वक 5 rows पाईं। cursor स्पष्ट रूप से बंद होने के तुरंत बाद %ROWCOUNT का value क्या होगा?

  1. 5
  2. 0
  3. NULL
  4. एक INVALID_CURSOR exception trigger करता है
Show the answer

एक INVALID_CURSOR exception trigger करता है

एक explicit cursor का कोई भी attribute इसके बंद होने के बाद access करना Oracle में एक तुरंत INVALID_CURSOR runtime error trigger करता है। एक बार बंद होने पर, cursor का memory context पूरी तरह नष्ट हो जाता है, इसके status flags अनुपलब्ध बनाते हुए।

Watch out

Implicit बनाम Explicit %NOTFOUND जाल

भारतीय BCA university laboratory practicals में एक classic ग़लती ग़लत cursor prefix इस्तेमाल करना है। अगर आप c_tickets नामक एक explicit cursor जाँच रहे हैं, आपको c_tickets%NOTFOUND इस्तेमाल करना होगा। अगर आप ग़लती से SQL%NOTFOUND लिखते हैं, Oracle आपके explicit cursor के बजाय पिछली implicit SQL query की स्थिति जाँचेगा! यह loops को ग़लत तरह execute या अनंत रूप से चलने का कारण बनता है क्योंकि आप ग़लत dashboard indicator query कर रहे हैं।

Think first

Mental Challenge: %ROWCOUNT की शुरुआती स्थिति

कोई FETCH statement execute होने से पहले पर एक explicit cursor सफलतापूर्वक open होने के ठीक बाद, आपको लगता है %ROWCOUNT कौन सा numeric value रखता है? tap करने से पहले जवाब मन में निकालिए।

Show the answer

यह 0 return करता है! जब आप एक cursor open करते हैं, Oracle query चलाता है और active set तैयार करता है, पर शून्य rows असल में आपके local variables में fetch हुई हैं। इसलिए, %ROWCOUNT 0 पर initialize होता है और हर बार एक FETCH operation सफलतापूर्वक एक record पाने पर 1 से बढ़ता है।

Theory

Database Audits और भविष्य का Synchronization

Cursor attributes विस्तृत management reports बनाते समय या production helpdesk environments में batch processing statistics track करते समय अहम हैं। आप अगले semester अपने Sem 3 BCA303 SQLite mobile database course में row-counting patterns पर भारी निर्भर करेंगे। जब sync routines एक remote master server से updates fetch करती हैं, आपका code mobile user interface screen पर progress percentages रँगने के लिए local loop counts इस्तेमाल करेगा।

Summary

Key takeaways

  • Cursor attributes विशेष boolean या numeric flags हैं जो execution state का वर्णन करते हैं।
  • एक OPEN या CLOSE command चलाने से पहले एक cursor सक्रिय है या नहीं सत्यापित करने के लिए %ISOPEN इस्तेमाल कीजिए।
  • %FOUND और %NOTFOUND flags दर्शाते हैं कि पिछला FETCH operation सफलतापूर्वक data लौटाया या नहीं।
  • %ROWCOUNT attribute cursor खुलने के बाद से process की rows की संचयी संख्या track करता है।
  • एक बंद explicit cursor पर attributes reference करने की कोशिश एक तुरंत exception error में परिणत होती है।
  • Memory hook: ISOPEN से खोलो, NOTFOUND से जाँचो, ROWCOUNT से गिनो, और बंद करने से पहले जाँचो!

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Cursors and Exception Handling, Packages, Triggers

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Cursor Attributes: %FOUND, %NOTFOUND, %ISOPEN, %ROWCOUNT · Mastering SQL - PL/SQL (SEC-02 option A) · Gri-Learn