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

Cursor

A cursor in SQL is a temporary work area that allows for the execution of SQL statements and processing of data one row at a time. There are two types of cursors: implicit, created automatically by the DBMS, and explicit, defined by the developer. The process of using a cursor involves four main statements: DECLARE, OPEN, FETCH, and CLOSE, which together facilitate the retrieval and manipulation of data from a database.

Uploaded by

lokibeaver
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Cursor

A cursor in SQL is a temporary work area that allows for the execution of SQL statements and processing of data one row at a time. There are two types of cursors: implicit, created automatically by the DBMS, and explicit, defined by the developer. The process of using a cursor involves four main statements: DECLARE, OPEN, FETCH, and CLOSE, which together facilitate the retrieval and manipulation of data from a database.

Uploaded by

lokibeaver
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 8

CURSOR

Definition :-
 A cursor is a temporary work area to execute SQL statements and to store the processed
information. (This work area is private to SQL’s operations and is called a CURSOR).
 To handle a result set inside the stored procedure, stored functions and triggers, we can use a
cursor.
 It is a way to retrieve data from a database and process it one row at a time, rather than
retrieving all the data at once and then processing it. (It can hold more than one row, but can
process only one row at a time).
 When a SELECT statement is executed, the result set is stored in a cursor in SQL, which can then
be used to fetch and process the rows one at a time. The cursor acts as a pointer to the current row
in the result set, allowing the developer to move forward and backwards through the result set.
 In SQL, there are two types of cursors:
 Implicit cursors: These are automatically created by the database management system (DBMS)
when a SELECT statement is executed without the use of an explicit cursor.
 Explicit cursors: These are created by the developer using the DECLARE CURSOR statement.
They are used to retrieve and process data.
 Cursors in SQL are commonly used in PL/SQL and T-SQL to fetch and process data from the
database, but they can also be used in other languages that have an SQL interface.
Diagram illustrates how cursor works :-

NO

DECLARE OPEN FETCH EMPTY CLOSE


YES

To work with cursors, there are 4 statements required. They are,


i) DECLARE Statement
ii) OPEN Statement
iii) FETCH Statement
iv) CLOSE Statement
i) DECLARE (CURSOR DEFINITION – DECLARE CURSOR):-
 This statement defines the cursor, gives it a name to it and assigns an SQL
statement to it. (It doesn’t execute but merely defines it)
 Cursor must be associated with SELECT statement.
Syntax :-
CURSOR cursor_name IS SELECT Statement;
Example.,
CURSOR c_emp IS SELECT * FROM employee_table;
ii) OPEN :-
 This is used to Open the cursor. This Open statement initializes the result set
returned by the query for the cursor. Hence we must call the OPEN statement
before fetching rows from the result set.
Syntax :-
OPEN cursor_name;
Example.,
OPEN c_emp;
iii) FETCH :-
 This statement returns the data from the result table one row at a time to
the host variables. (i.e) it retrieves the next row pointed by the cursor and
move the cursor to the next row in the result set.
Syntax :-
FETCH cursor_name INTO variableslist;
Example,
FETCH c_emp INTO c_name, c_salary, c_deductions ;
iv) CLOSE :-
 This releases all resources used by the cursor. When the cursor is no longer
used, we should close it.
Syntax :-
CLOSE cursor_name ;
Example,
CLOSE c_emp;
 For Deallocating the memory, DEALLOCATE keyword is used.
 The SQL cursor, which always has the attributes like %FOUND,
%ISOPEN, %NOTFOUND, and %ROWCOUNT.

 %FOUND Returns ---- TRUE if an INSERT, UPDATE, or DELETE


statement affected one or more rows or a SELECT INTO statement returned
one or more rows. Otherwise, it returns FALSE.
 %NOTFOUND ---- The logical opposite of %FOUND. It returns TRUE if an
INSERT, UPDATE, or DELETE statement affected no rows, or a SELECT
INTO statement returned no rows. Otherwise, it returns FALSE.
 %ISOPEN ----- Always returns FALSE for implicit cursors, because Oracle
closes the SQL cursor automatically after executing its associated SQL
statement.
 %ROWCOUNT---- Returns the number of rows affected by an INSERT,
UPDATE, or DELETE statement, or returned by a SELECT INTO statement.
Example for Cursor,
DECLARE
CURSOR c_emp IS
select empname, salary, deduction FROM employee_table;
c_name varchar2(20);
c_salary number(7,2) : = 0;
c_deduction number(7,2) : = 0;
net_salary number(7,2) : = 0;
BEGIN
OPEN c_emp
LOOP
FETCH c_emp INTO c_name, c_salary, c_deduction ;
EXIT WHEN c_emp%NOTFOUND;
net_salary = c_salary - c_deduction ;
INSERT INTO salary_table VALUES (c_name, net_salary) ;
END LOOP ;
CLOSE c_emp ;
COMMIT ;
END ;
1. DECLARE Section (Variable Declarations) :-
Cursor c_emp: This cursor selects data from the employee_table. It retrieves the employee's name
(empname), salary, and deduction.
Variable declarations : (c_name, c_salary, c_deduction, net_salary)
2. BEGIN Section:-
 OPEN c_emp: This opens the cursor c_emp and starts fetching rows from the employee_table.
 LOOP: This loop processes each record retrieved by the cursor.
 FETCH c_emp INTO c_name, c_salary, c_deductions: This fetches each row from the cursor and
stores the empname in c_name, salary in c_salary, and deduction in c_deductions.
 EXIT WHEN c_emp%NOTFOUND: This exits the loop when there are no more rows to fetch.
 Calculation of net_salary:
net_salary = c_salary - c_deduction: For each employee, it calculates the net salary by subtracting
the deduction from the salary.
 INSERT INTO salary_table: The calculated net_salary and the employee's name are inserted into
the salary_table. This is where the final data is stored.
 CLOSE c_emp: After processing all records, the cursor is closed to free up resources.
 COMMIT: This commits the transaction, saving all the changes (inserts) made to the database.
 This PL/SQL block processes each employee record from the
employee_table, calculates the net salary by subtracting deduction from
the salary, and then inserts the net salary into the salary_table. The process
is done for all employees in the employee_table using cursor.

You might also like