0% found this document useful (0 votes)
19 views

Cursor

Uploaded by

Sangram Harpale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Cursor

Uploaded by

Sangram Harpale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

CREATE TABLE STUDENT (

STUDENT_ID NUMBER PRIMARY KEY,

FIRST_NAME VARCHAR2(50),

LAST_NAME VARCHAR2(50),

AGE NUMBER,

GRADE CHAR(2)

);

INSERT INTO STUDENT (STUDENT_ID, FIRST_NAME, LAST_NAME, AGE, GRADE) VALUES (1, 'John', 'Doe',
20, 'A');

INSERT INTO STUDENT (STUDENT_ID, FIRST_NAME, LAST_NAME, AGE, GRADE) VALUES (2, 'Jane',
'Smith', 21, 'B');

INSERT INTO STUDENT (STUDENT_ID, FIRST_NAME, LAST_NAME, AGE, GRADE) VALUES (3, 'Jim', 'Brown',
19, 'C');

COMMIT; -- Make sure to commit the transaction

IMPLICIT CURSOR

DECLARE

v_first_name STUDENT.FIRST_NAME%TYPE;

v_last_name STUDENT.LAST_NAME%TYPE;

v_age STUDENT.AGE%TYPE;

v_grade STUDENT.GRADE%TYPE;

BEGIN

-- Implicit cursor for selecting all students

FOR student_record IN (SELECT FIRST_NAME, LAST_NAME, AGE, GRADE FROM STUDENT) LOOP

v_first_name := student_record.FIRST_NAME;

v_last_name := student_record.LAST_NAME;

v_age := student_record.AGE;

v_grade := student_record.GRADE;

-- Display the student information


DBMS_OUTPUT.PUT_LINE('Name: ' || v_first_name || ' ' || v_last_name ||

', Age: ' || v_age || ', Grade: ' || v_grade);

END LOOP;

END;

EXAMPLE 2

Begin

for e_record in ( select * from student)

loop

dbms_output.put_line('Name: '|| e_record.FIRST_NAME

|| ' '|| e_record.LAST_NAME ||', Age: ' || e_record.AGE

||', Roll no: ' ||e_record.STUDENT_ID);

End loop;

End;

You define a cursor, open it, fetch rows from it, and then close it when done.

select * from STUDENT;

Steps to Use an Explicit Cursor

Declare the Cursor: Define the SQL query to be executed.

Open the Cursor: Allocate memory and execute the SQL statement.

Fetch from the Cursor: Retrieve rows one by one.

Close the Cursor: Release the memory allocated for the cursor.

DECLARE

CURSOR student_cursor IS

SELECT FIRST_NAME, LAST_NAME, AGE, STUDENT_ID FROM STUDENT;


v_first_name STUDENT.FIRST_NAME%TYPE;

v_last_name STUDENT.LAST_NAME%TYPE;

v_age STUDENT.AGE%TYPE;

v_student_id STUDENT.STUDENT_ID%TYPE;

BEGIN

-- Open the cursor

OPEN student_cursor;

-- Fetch each row from the cursor

LOOP

FETCH student_cursor INTO v_first_name, v_last_name, v_age, v_student_id;

EXIT WHEN student_cursor%NOTFOUND; -- Exit loop if no more rows

-- Print student details

DBMS_OUTPUT.PUT_LINE('Name: ' || v_first_name || ' ' || v_last_name ||

', Age: ' || v_age ||

', Rollno: ' || v_student_id);

END LOOP;

-- Close the cursor

CLOSE student_cursor;

END;

Implicit Cursor

Automatic: Oracle automatically creates this when you run a SQL query.

Less Control: You can’t control it as much; it’s just a quick way to fetch data.
Usage: Ideal for simple queries where you don’t need to manage the cursor lifecycle.

Example: You directly run a loop over a SQL query and print results. No need to declare or manage the
cursor.

Explicit Cursor

Manual: You define it yourself in your code.

More Control: You can open, fetch, and close it. This gives you more flexibility, especially for complex
queries.

Usage: Useful when you need to process rows one at a time or perform additional logic between
fetches.

Example: You declare the cursor, open it, fetch data in a loop, and then close it when done.

Key Points

Output: Both types can produce the same output, but explicit cursors let you manage the fetching
process more precisely.

When to Use:

Use implicit cursors for simple queries.

Use explicit cursors for more control and complex logic

You might also like