Answered step by step
Verified Expert Solution
Question
00
1 Approved Answer
Lab 5 : Cursors in PL / SQL LAB 4 Content PL / SQL - Cursors In this chapter, we will discuss the cursors in
Lab : Cursors in PLSQL LAB Content PLSQL Cursors In this chapter, we will discuss the cursors in PLSQL Oracle creates a memory area, known as the context area, for processing an SQL statement, which contains all the information needed for processing the statement; for example, the number of rows processed, etc. Acursoris a pointer to this context area. PLSQL controls the context area through a cursor. A cursor holds the rows one or more returned by a SQL statement. The set of rows the cursor holds is referred to as theactive set. You can name a cursor so that it could be referred to in a program to fetch and process the rows returned by the SQL statement, one at a time. There are two types of cursors Implicit cursors Explicit cursors Implicit Cursors Implicit cursors are automatically created by Oracle whenever an SQL statement is executed, when there is no explicit cursor for the statement. Programmers cannot control the implicit cursors and the information in it Whenever a DML statement INSERT UPDATE and DELETE is issued, an implicit cursor is associated with this statement. For INSERT operations, the cursor holds the data that needs to be inserted. For UPDATE and DELETE operations, the cursor identifies the rows that would be affected. In PLSQL you can refer to the most recent implicit cursor as theSQL cursor, which always has attributes such asFOUND, ISOPEN, NOTFOUND, andROWCOUNT. The SQL cursor has additional attributes,BULKROWCOUNTandBULKEXCEPTIONS, designed for use with theFORALLstatement The following table provides the description of the most used attributes SNo Attribute & Description 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. Any SQL cursor attribute will be accessed assqlattributenameas shown below in the example. Example We will be using the CUSTOMERS table we had created and used in the previous chapters. Select from customers; ID NAME AGE ADDRESS SALARY Ramesh Ahmedabad Khilan Delhi kaushik Kota Chaitali Mumbai Hardik Bhopal Komal MP The following program will update the table and increase the salary of each customer by and use theSQLROWCOUNTattribute to determine the number of rows affected DECLARE totalrows number; BEGIN UPDATE customers SET salary salary ; IF sqlnotfound THEN dbmsoutput.putlineno customers selected'; ELSIF sqlfound THEN totalrows : sqlrowcount; dbmsoutput.putline totalrows customers selected ; END IF; END; When the above code is executed at the SQL prompt, it produces the following result customers selected PLSQL procedure successfully completed. If you check the records in customers table, you will find that the rows have been updated Select from customers; ID NAME AGE ADDRESS SALARY Ramesh Ahmedabad Khilan Delhi kaushik Kota Chaitali Mumbai Hardik Bhopal Komal MP Explicit Cursors Explicit cursors are programmerdefined cursors for gaining more control over thecontext area. An explicit cursor should be defined in the declaration section of the PLSQL Block. It is created on a SELECT Statement which returns more than one row. The syntax for creating an explicit cursor is CURSOR cursorname IS selectstatement; Working with an explicit cursor includes the following steps Declaring the cursor for initializing the memory Opening the cursor for allocating the memory Fetching the cursor for retrieving the data Closing the cursor to release the allocated memory Declaring
Lab : Cursors in PLSQL
LAB Content
PLSQL Cursors
In this chapter, we will discuss the cursors in PLSQL Oracle creates a memory area, known as the context area, for processing an SQL statement, which contains all the information needed for processing the statement; for example, the number of rows processed, etc.
Acursoris a pointer to this context area. PLSQL controls the context area through a cursor. A cursor holds the rows one or more returned by a SQL statement. The set of rows the cursor holds is referred to as theactive set.
You can name a cursor so that it could be referred to in a program to fetch and process the rows returned by the SQL statement, one at a time. There are two types of cursors
Implicit cursors
Explicit cursors
Implicit Cursors
Implicit cursors are automatically created by Oracle whenever an SQL statement is executed, when there is no explicit cursor for the statement. Programmers cannot control the implicit cursors and the information in it
Whenever a DML statement INSERT UPDATE and DELETE is issued, an implicit cursor is associated with this statement. For INSERT operations, the cursor holds the data that needs to be inserted. For UPDATE and DELETE operations, the cursor identifies the rows that would be affected.
In PLSQL you can refer to the most recent implicit cursor as theSQL cursor, which always has attributes such asFOUND, ISOPEN, NOTFOUND, andROWCOUNT. The SQL cursor has additional attributes,BULKROWCOUNTandBULKEXCEPTIONS, designed for use with theFORALLstatement The following table provides the description of the most used attributes
SNo Attribute & Description
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.
Any SQL cursor attribute will be accessed assqlattributenameas shown below in the example.
Example
We will be using the CUSTOMERS table we had created and used in the previous chapters.
Select from customers;
ID NAME AGE ADDRESS SALARY
Ramesh Ahmedabad
Khilan Delhi
kaushik Kota
Chaitali Mumbai
Hardik Bhopal
Komal MP
The following program will update the table and increase the salary of each customer by and use theSQLROWCOUNTattribute to determine the number of rows affected
DECLARE
totalrows number;
BEGIN
UPDATE customers
SET salary salary ;
IF sqlnotfound THEN
dbmsoutput.putlineno customers selected';
ELSIF sqlfound THEN
totalrows : sqlrowcount;
dbmsoutput.putline totalrows customers selected ;
END IF;
END;
When the above code is executed at the SQL prompt, it produces the following result
customers selected
PLSQL procedure successfully completed.
If you check the records in customers table, you will find that the rows have been updated
Select from customers;
ID NAME AGE ADDRESS SALARY
Ramesh Ahmedabad
Khilan Delhi
kaushik Kota
Chaitali Mumbai
Hardik Bhopal
Komal MP
Explicit Cursors
Explicit cursors are programmerdefined cursors for gaining more control over thecontext area. An explicit cursor should be defined in the declaration section of the PLSQL Block. It is created on a SELECT Statement which returns more than one row.
The syntax for creating an explicit cursor is
CURSOR cursorname IS selectstatement;
Working with an explicit cursor includes the following steps
Declaring the cursor for initializing the memory
Opening the cursor for allocating the memory
Fetching the cursor for retrieving the data
Closing the cursor to release the allocated memory
Declaring
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started