SQL Cursor in Hindi | SQL में Cursor क्या होता है?

SQL में जब हम किसी query को execute करते हैं, तो उसके result को row-by-row process करने की आवश्यकता होती है।
ऐसे में SQL एक खास mechanism का उपयोग करता है जिसे कहते हैं Cursor (कर्सर)

 

Cursor क्या होता है? (What is Cursor in SQL)

जब कोई SQL statement execute होता है,
तो Oracle या SQL Server एक special memory area create करता है जिसे कहते हैं Context Area।

यह area SQL query से जुड़े records और information को temporarily store करता है।
और इस memory area को access या control करने के लिए Cursor का इस्तेमाल किया जाता है।

सरल शब्दों में:

“Cursor SQL में एक pointer होता है जो query result को row-by-row access करने के लिए use किया जाता है।”

SQL Cursor in Hindi
SQL Cursor in Hindi

Cursor का कार्य (How Cursor Works)

जब कोई query execute होती है

  1. Database एक context area (memory) बनाता है
  2. Cursor उस area को point करता है
  3. Cursor result को एक-एक करके fetch करता है
  4. Programmer cursor से data को read या modify कर सकता है

इस तरह Cursor हमें SQL result को step-by-step process करने की सुविधा देता है।

 

SQL Cursor के प्रकार (Types of SQL Cursors)

SQL में Cursor दो प्रकार के होते हैं

1. Implicit Cursor

  • यह automatically create हो जाता है जब कोई SQL statement run होती है।
  • User को इसे manually define या open करने की जरूरत नहीं होती।
  • यह छोटे queries (जैसे INSERT, UPDATE, DELETE) के लिए useful होता है।
  • Programmers implicit cursor के internal data को control नहीं कर सकते।

Example:
जब आप UPDATE Employees SET Salary = Salary + 1000; चलाते हैं,
तो SQL अपने आप implicit cursor बना देता है ताकि updated rows को track किया जा सके।

 

2. Explicit Cursor

  • यह user-defined cursor होता है।
  • इसे manually declare, open, fetch, और close करना होता है।
  • जब हमें query के result set पर row-by-row operation करना होता है,
    तब Explicit Cursor का प्रयोग किया जाता है।

 

Explicit Cursor का Syntax

CURSOR cursor_name IS
SELECT column1, column2, ...
FROM table_name
WHERE condition;

यहाँ cursor_name user-defined नाम है, और query वो records fetch करती है
जिन्हें हम बाद में cursor के माध्यम से row-by-row process करेंगे।

 

Explicit Cursor Management Steps

Explicit Cursor को manage करने के लिए चार basic steps होते हैं-

StepDescription
Declare CursorCursor और उसकी SQL query को define करें
Open CursorCursor को activate करें ताकि records access हो सकें
Fetch DataCursor से एक-एक करके data fetch करें
Close CursorMemory release करने के लिए cursor को बंद करें

 

Cursor Attributes (कर्सर के मुख्य Attributes)

AttributeDescription
%ISOPENअगर cursor open है तो TRUE लौटाता है, वरना FALSE
%FOUNDअगर cursor ने कोई record fetch किया है तो TRUE लौटाता है
%NOTFOUNDअगर कोई record नहीं मिला तो TRUE लौटाता है
%ROWCOUNTअब तक fetch किए गए rows की संख्या बताता है

 

Example – Explicit Cursor Program

DECLARE
   CURSOR emp_cursor IS
   SELECT EmpName, Salary FROM Employees WHERE Department = 'Sales';

   emp_record Employees%ROWTYPE;
BEGIN
   OPEN emp_cursor;
   LOOP
      FETCH emp_cursor INTO emp_record;
      EXIT WHEN emp_cursor%NOTFOUND;
      DBMS_OUTPUT.PUT_LINE('Employee: ' || emp_record.EmpName || 
                           ' Salary: ' || emp_record.Salary);
   END LOOP;
   CLOSE emp_cursor;
END;
 
Explanation:
  • यहाँ emp_cursor नाम का cursor बनाया गया है।
  • यह Sales department के सभी employees को fetch करता है।
  • Loop में एक-एक करके rows पढ़ी जाती हैं।
  • अंत में cursor को close कर दिया जाता है।

 

Cursor के फायदे (Advantages of Cursor)

लाभविवरण
Row-wise processingएक-एक record पर operation कर सकते हैं
ControlResult set पर अधिक नियंत्रण मिलता है
Dynamic logicComplex PL/SQL logic लागू कर सकते हैं
Memory managementControlled way में data fetch होता है

 

Cursor के नुकसान (Disadvantages)

  • Performance slow होती है (row-by-row processing की वजह से)
  • ज्यादा memory consume करता है
  • Large data set के साथ inefficient होता है
  • Complex PL/SQL code बढ़ जाता है

 

Implicit vs Explicit Cursor Comparison

FeatureImplicit CursorExplicit Cursor
CreationAutomaticallyManually by user
ControlLimitedFull control
UsageSingle SQL statementsMultiple row processing
ExampleUPDATE, DELETE, INSERTSELECT queries with multiple rows

 

इन्हें भी पढ़े –

  1. DBMS क्या है DBMS के प्रकार और कार्य की पूरी जानकारी | DBMS In Hindi
  2. डेटाबेस यूजर्स क्या है?
  3. डेटा इंडिपेंडेंस क्या है
  4. DBMS के लाभ
  5. Characteristics Of DBMS In Hindi
  6. डेटा मॉडल क्या है?
  7. डेटाबेस स्कीमा क्या है
  8. इंस्टैंस क्या है
  9. डेटाबेस इंटरफ़ेस क्या है
  10. डेटाबेस लैंग्वेज क्या है
  11. Classification of DBMS in Hindi
  12. Entity Set in DBMS
  13. DBMS Architecture in Hindi
  14. Types of Data Models in Hindi
  15. Attributes in DBMS in Hindi
  16. Entity Set in DBMS (Hindi)
  17. Entity Types in DBMS (Hindi)
  18. Types of Attributes in DBMS
  19. ER Model in DBMS (E-R मॉडल) क्या है?
  20. Entities के बीच संबंध
  21. Domain in DBMS
  22. Tuples in DBMS
  23. SQL में Joins क्या हैं?
  24. Primary Key in DBMS
  25. DBMS Keys in Hindi
  26. DBMS Data Integrity in Hindi
  27. Relational Algebra in DBMS
  28. Normalization in DBMS क्या है?
  29. BCNF (Boyce-Codd Normal Form) in DBMS
  30. Functional Dependency in DBMS
  31. Non-Loss Decomposition in DBMS
  32. SQL Data Types in Hindi
  33. Create Table in SQL in Hindi
  34. SQL DROP TABLE और ALTER TABLE in Hindi
  35. SQL Indexes in Hindi
  36. SDLC in Hindi
  37. DBLC in Hindi
  38. SQL Views in Hindi
  39. PL/SQL PROCEDURES in hindi
  40. Database Normalization in Hindi
  41. Domain Key Normal Form in Hindi
  42. SQL Objects in Hindi
  43. Aggregation, Generalization, Specialization and Association in Hindi
  44. Data Dictionary क्या है?
  45. डेटा वेयरहाउसिंग क्या है?
  46. डाटा माइनिंग क्या है?

 

RivnTech Pro Tip:

अगर आपको performance बढ़ानी है,
तो cursor का इस्तेमाल छोटे datasets या complex logic वाली queries के लिए करें।
Large data process करने के लिए bulk operations या cursor FOR loops ज़्यादा बेहतर हैं।

 

निष्कर्ष (Conclusion)

Cursor SQL का एक powerful tool है जो query results को row-by-row process करने की सुविधा देता है।
यह दो प्रकार का होता है – Implicit और Explicit।
हालांकि इसका उपयोग सावधानी से करना चाहिए क्योंकि यह memory और performance को प्रभावित कर सकता है।

Leave a Comment