![]() |
PL/SQL stands for Procedural Language/ Structured Query Language. It has block structure programming features.PL/SQL supports SQL queries. It also supports the declaration of the variables, control statements, Functions, Cursor, Procedure, and Triggers.PL/SQL contains declaration section, execution section, exception section, and end. Declare and exception blocks are optional. In this article, we will cover the PL/SQL Cursors, with a particular focus on Updatable Cursors. Cursors in PL/SQL are pivotal for handling result sets, and there are two main types: read-only and updatable. Read-only cursors allow data retrieval but prohibit modifications, while updatable cursors extend this functionality, permitting both fetching and updating of data. PL/SQL CursorsContext area is a special memory that is used by Oracle for Storing and retrieving information from the database. It contains all the details related to the database. The cursor is a virtual pointer to the context area, in the database. The cursor helps to process through the rows one by one. It can be used implicitly or explicitly. Implicit cursor is used without declaring the cursor with a cursor name in the declaration section. The maximum number of characters allowed in a cursor name is 128. The cursor name should be unique. An explicit cursor is declared with a cursor name and the SELECT statement. There are two types of cursor, read-only and updatable. The Read-OnlyThe read-only cursor cannot modify the data. It is declared with the FOR READ ONLY keyword in the SELECT statement. Usually FOR READ ONLY keyword is optional as the SELECT statement performs read-only operations. Updatable CursorUpdatable cursors are used to perform manipulation operations such as deleting, inserting, and updating rows. FOR UPDATE keyword is used in cursor declaration. Updation can be performed with FOR UPDATE and FOR UPDATE OF keyword. FOR UPDATE locks the selected row so another transaction can’t access it.FOR UPDATE OF is used to provide the column for which the row should be locked. Syntax:
Examples of PL/SQL Updatable CursorsExample 1: Updating Geeks Table Scores Using For UPDATE in PL/SQLUpdate the score from the Geeks table using FOR UPDATE. Query: SET SERVEROUTPUT ON; Explanation: The Cursor with FOR UPDATE keyword is declared in the declaration section. It is opened and fetched in the execution section. The Fetch operation is performed in loop to update each row UPDATE keyword is used to update the table.” WHERE CURRENT OF cursor_geek” is used to point to the current row. “cursor_geek%NOTFOUND” is used as can condition till there are rows in the table. The cursor is closed after the updation. Output: The output contains the table before the update and the table after updation. ![]() PL/SQL UPDATABLE CURSOR Example 2: Updating Geeks Table Scores Using For UPDATE OF in PL/SQLUpdate the score from the Geeks table using FOR UPDATE OF. Syntax:
Query: SET SERVEROUTPUT ON; Output: ![]() PL/SQL UPDATABLE CURSOR Explanation: FOR UPDATE OF keyword is used to lock the score column from the geeks table. Advantages and Disadvantages of the Updatable CursorsAdvantages
Disadvantages
ConclusionPL/SQL Updatable Cursors provide a powerful way to update multiple rows. It helps to increases the performance. It helps to make an efficient and effective Database Management System. By familiarizing yourself with the syntax, examining real-world examples, and considering the advantages and disadvantages, developers can use updateable cursors wisely, improving the performance and functionality of PL/SQL applications. |
Reffered: https://www.geeksforgeeks.org
Databases |
Related |
---|
![]() |
![]() |
![]() |
|
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |