SlideShare a Scribd company logo
PLSQL_cur.pptx presentation uploadtttees
PL/SQL Cursor
• A cursor is a pointer that points to a result of a query.
• PL/SQL has two types of cursors:
• implicit cursors and
• explicit cursors.
Implicit cursors
• Whenever Oracle executes an SQL statement such as SELECT INTO,
INSERT, UPDATE, and DELETE, it automatically creates an implicit
cursor.
Explicit cursors
• An explicit cursor is a SELECT statement declared explicitly in the
declaration section of the current block or a package specification.
The following illustration shows the execution
cycle of an explicit cursor:
Declare a cursor
• Before using an explicit cursor, you must declare it in the declaration
section of a block or package as follows:
CURSOR cursor_name IS query;
Open a cursor
• Before start fetching rows from the cursor, you must open it. To open
a cursor, you use the following syntax:
OPEN cursor_name;
Fetch from a cursor
• The FETCH statement places the contents of the current row into
variables. The syntax of FETCH statement is as follows:
FETCH cursor_name INTO variable_list;
Closing a cursor
After fetching all rows, you need to close the cursor with the CLOSE
statement:
CLOSE cursor_name;
Explicit Cursor Attributes
A cursor has four attributes which you can reference in the following
format:
cursor_name%attribute
Where cursor_name is the name of the explicit cursor.
1) %ISOPEN
This attribute is TRUE if the cursor is open or FALSE if it is not.
2) %FOUND
This attribute has four values:
•NULL before the first fetch
•TRUE if a record was fetched successfully
•FALSE if no row is returned
•INVALID_CURSOR if the cursor is not opened
3) %NOTFOUND
This attribute has four values:
•NULL before the first fetch
•FALSE if a record was fetched successfully
•TRUE if no row is returned
•INVALID_CURSOR if the cursor is not opened
3) %ROWCOUNT
The %ROWCOUNT attribute returns the number of rows fetched from
the cursor.
If the cursor is not opened, this attribute returns INVALID_CURSOR.
CREATE VIEW sales AS
SELECT customer_id,
SUM(unit_price * quantity) total,
ROUND(SUM(unit_price * quantity) * 0.05)
credit
FROM order_items
INNER JOIN orders USING (order_id)
WHERE status = 'Shipped'
GROUP BY customer_id;
DECLARE
l_budget NUMBER := 1000000;
-- cursor
CURSOR c_sales IS
SELECT * FROM sales
ORDER BY total DESC;
-- record
r_sales c_sales%ROWTYPE;
BEGIN
-- reset credit limit of all customers
UPDATE customers SET
credit_limit = 0;
OPEN c_sales;
LOOP
FETCH c_sales INTO r_sales;
EXIT WHEN c_sales%NOTFOUND;
-- update credit for the current customer
UPDATE
customers
SET
credit_limit =
CASE WHEN l_budget >
r_sales.credit
THEN r_sales.credit
ELSE l_budget
END
WHERE
customer_id =
r_sales.customer_id;
-- reduce the budget for credit limit
l_budget := l_budget -
r_sales.credit;
DBMS_OUTPUT.PUT_LINE( 'Custom
er id: ' ||r_sales.customer_id ||
' Credit: ' || r_sales.credit || '
Remaining Budget: ' || l_budget );
-- check the budget
EXIT WHEN l_budget <= 0;
END LOOP;
CLOSE c_sales;
END;
Creating a trigger in Oracle
CREATE [OR REPLACE] TRIGGER trigger_name
{BEFORE | AFTER } triggering_event ON table_name
[FOR EACH ROW]
[FOLLOWS | PRECEDES another_trigger]
[ENABLE / DISABLE ]
[WHEN condition]
DECLARE
declaration statements
BEGIN
executable statements
EXCEPTION
exception_handling statements
END;
CREATE [OR REPLACE] TRIGGER trigger_name
{BEFORE | AFTER } triggering_event ON table_name
[FOR EACH ROW]
[FOLLOWS | PRECEDES another_trigger]
[ENABLE / DISABLE ]
[WHEN condition]
DECLARE
declaration statements
BEGIN
executable statements
EXCEPTION
exception_handling statements
END;
CREATE TABLE audits (
audit_id NUMBER GENERATED BY DEFAULT AS IDENTITY
PRIMARY KEY,
table_name VARCHAR2(255),
transaction_name VARCHAR2(10),
by_user VARCHAR2(30),
transaction_date DATE
);
CREATE OR REPLACE TRIGGER customers_audit_trg
AFTER
UPDATE OR DELETE
ON customers
FOR EACH ROW
DECLARE
l_transaction VARCHAR2(10);
BEGIN
-- determine the transaction type
l_transaction := CASE
WHEN UPDATING THEN 'UPDATE'
WHEN DELETING THEN 'DELETE'
END;
-- insert a row into the audit table
INSERT INTO audits (table_name, transaction_name, by_user, transaction_date)
VALUES('CUSTOMERS', l_transaction, USER, SYSDATE);
END;
/

More Related Content

PPTX
PPT
Les21[1]Writing Explicit Cursors
PPTX
Day 6.pptx
PDF
Cursors
PPT
PPTX
PL_SQL - II.pptx
PDF
Lecture Notes Unit5 chapter19 Cursor in Pl/SQL
PDF
Pl sql-ch2
Les21[1]Writing Explicit Cursors
Day 6.pptx
Cursors
PL_SQL - II.pptx
Lecture Notes Unit5 chapter19 Cursor in Pl/SQL
Pl sql-ch2

Similar to PLSQL_cur.pptx presentation uploadtttees (20)

PPTX
PL/SQL - CURSORS
PPTX
Oracle: Cursors
PPTX
Oracle:Cursors
PPTX
plsql tutorialhub....
PPT
Cursors in oracle
PPT
PLplsql study aboutmsnsjskakajsjslajwvsbsns
PPT
Triggers n Cursors.ppt
PPTX
BBarters_PL_SQL gives cdemo details.pptx
PPT
Cursores explicitos
PPT
Basic cursors in oracle
PPT
PPT
Les18[1]Interacting with the Oracle Server
PPTX
PL_SQL_1.pptx fvbxcfbhxdfgh .
PPT
PL/SQL Stored Procedures And Cursors.ppt
PPTX
DBMS UNIT 9.pptx..................................
PDF
PPT
KMUTNB - Internet Programming 6/7
PPTX
Crime record management system project.pptx
DOC
3963066 pl-sql-notes-only
PPT
Lecture 3. MS SQL. Cursors.
PL/SQL - CURSORS
Oracle: Cursors
Oracle:Cursors
plsql tutorialhub....
Cursors in oracle
PLplsql study aboutmsnsjskakajsjslajwvsbsns
Triggers n Cursors.ppt
BBarters_PL_SQL gives cdemo details.pptx
Cursores explicitos
Basic cursors in oracle
Les18[1]Interacting with the Oracle Server
PL_SQL_1.pptx fvbxcfbhxdfgh .
PL/SQL Stored Procedures And Cursors.ppt
DBMS UNIT 9.pptx..................................
KMUTNB - Internet Programming 6/7
Crime record management system project.pptx
3963066 pl-sql-notes-only
Lecture 3. MS SQL. Cursors.
Ad

More from RamaKrishnaErroju (18)

PPSX
Stack-data-structure.ppsxErwewwwrrterewewew
PPT
Fourier-Series SAMPLE PRESENTATION FOR LEARNING
PPT
311introductiontomachinelearning.ppt12345
PPTX
Day17.pptx department of computer science and eng
PPTX
Day15.pptx school of computer science and ai
PPTX
Day3 datamining recent trends and advancements
PPTX
Day2 Applications of datamining using differe
PPTX
Introduction about Applications of data mining
PPTX
Googlecolab1 tutorial for data science practise
PPTX
Data Preprocessing techniques for applications
DOCX
TEXMAKER Overview research plagrism check
DOCX
BioIn_Pap1dramaprevenbtionsakcnnshejjsja
PPT
5261506.ppt
PPTX
CONVERSATION.pptx
PPT
sap-overview.ppt
PPT
PPTX
CONVERSATION.pptx
PPTX
Stack-data-structure.ppsxErwewwwrrterewewew
Fourier-Series SAMPLE PRESENTATION FOR LEARNING
311introductiontomachinelearning.ppt12345
Day17.pptx department of computer science and eng
Day15.pptx school of computer science and ai
Day3 datamining recent trends and advancements
Day2 Applications of datamining using differe
Introduction about Applications of data mining
Googlecolab1 tutorial for data science practise
Data Preprocessing techniques for applications
TEXMAKER Overview research plagrism check
BioIn_Pap1dramaprevenbtionsakcnnshejjsja
5261506.ppt
CONVERSATION.pptx
sap-overview.ppt
CONVERSATION.pptx
Ad

Recently uploaded (20)

PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
additive manufacturing of ss316l using mig welding
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPT
Project quality management in manufacturing
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPT
Mechanical Engineering MATERIALS Selection
PPTX
web development for engineering and engineering
PPTX
Welding lecture in detail for understanding
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
PPT on Performance Review to get promotions
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Construction Project Organization Group 2.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
additive manufacturing of ss316l using mig welding
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
bas. eng. economics group 4 presentation 1.pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Project quality management in manufacturing
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Mechanical Engineering MATERIALS Selection
web development for engineering and engineering
Welding lecture in detail for understanding
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
R24 SURVEYING LAB MANUAL for civil enggi
Automation-in-Manufacturing-Chapter-Introduction.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPT on Performance Review to get promotions
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Construction Project Organization Group 2.pptx

PLSQL_cur.pptx presentation uploadtttees

  • 2. PL/SQL Cursor • A cursor is a pointer that points to a result of a query. • PL/SQL has two types of cursors: • implicit cursors and • explicit cursors. Implicit cursors • Whenever Oracle executes an SQL statement such as SELECT INTO, INSERT, UPDATE, and DELETE, it automatically creates an implicit cursor. Explicit cursors • An explicit cursor is a SELECT statement declared explicitly in the declaration section of the current block or a package specification.
  • 3. The following illustration shows the execution cycle of an explicit cursor:
  • 4. Declare a cursor • Before using an explicit cursor, you must declare it in the declaration section of a block or package as follows: CURSOR cursor_name IS query; Open a cursor • Before start fetching rows from the cursor, you must open it. To open a cursor, you use the following syntax: OPEN cursor_name; Fetch from a cursor • The FETCH statement places the contents of the current row into variables. The syntax of FETCH statement is as follows: FETCH cursor_name INTO variable_list;
  • 5. Closing a cursor After fetching all rows, you need to close the cursor with the CLOSE statement: CLOSE cursor_name; Explicit Cursor Attributes A cursor has four attributes which you can reference in the following format: cursor_name%attribute
  • 6. Where cursor_name is the name of the explicit cursor. 1) %ISOPEN This attribute is TRUE if the cursor is open or FALSE if it is not. 2) %FOUND This attribute has four values: •NULL before the first fetch •TRUE if a record was fetched successfully •FALSE if no row is returned •INVALID_CURSOR if the cursor is not opened 3) %NOTFOUND This attribute has four values: •NULL before the first fetch •FALSE if a record was fetched successfully •TRUE if no row is returned •INVALID_CURSOR if the cursor is not opened 3) %ROWCOUNT The %ROWCOUNT attribute returns the number of rows fetched from the cursor. If the cursor is not opened, this attribute returns INVALID_CURSOR.
  • 7. CREATE VIEW sales AS SELECT customer_id, SUM(unit_price * quantity) total, ROUND(SUM(unit_price * quantity) * 0.05) credit FROM order_items INNER JOIN orders USING (order_id) WHERE status = 'Shipped' GROUP BY customer_id;
  • 8. DECLARE l_budget NUMBER := 1000000; -- cursor CURSOR c_sales IS SELECT * FROM sales ORDER BY total DESC; -- record r_sales c_sales%ROWTYPE; BEGIN -- reset credit limit of all customers UPDATE customers SET credit_limit = 0; OPEN c_sales; LOOP FETCH c_sales INTO r_sales; EXIT WHEN c_sales%NOTFOUND; -- update credit for the current customer UPDATE customers SET credit_limit = CASE WHEN l_budget > r_sales.credit THEN r_sales.credit ELSE l_budget END WHERE customer_id = r_sales.customer_id; -- reduce the budget for credit limit l_budget := l_budget - r_sales.credit; DBMS_OUTPUT.PUT_LINE( 'Custom er id: ' ||r_sales.customer_id || ' Credit: ' || r_sales.credit || ' Remaining Budget: ' || l_budget ); -- check the budget EXIT WHEN l_budget <= 0; END LOOP; CLOSE c_sales; END;
  • 9. Creating a trigger in Oracle CREATE [OR REPLACE] TRIGGER trigger_name {BEFORE | AFTER } triggering_event ON table_name [FOR EACH ROW] [FOLLOWS | PRECEDES another_trigger] [ENABLE / DISABLE ] [WHEN condition] DECLARE declaration statements BEGIN executable statements EXCEPTION exception_handling statements END;
  • 10. CREATE [OR REPLACE] TRIGGER trigger_name {BEFORE | AFTER } triggering_event ON table_name [FOR EACH ROW] [FOLLOWS | PRECEDES another_trigger] [ENABLE / DISABLE ] [WHEN condition]
  • 12. CREATE TABLE audits ( audit_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, table_name VARCHAR2(255), transaction_name VARCHAR2(10), by_user VARCHAR2(30), transaction_date DATE );
  • 13. CREATE OR REPLACE TRIGGER customers_audit_trg AFTER UPDATE OR DELETE ON customers FOR EACH ROW DECLARE l_transaction VARCHAR2(10); BEGIN -- determine the transaction type l_transaction := CASE WHEN UPDATING THEN 'UPDATE' WHEN DELETING THEN 'DELETE' END; -- insert a row into the audit table INSERT INTO audits (table_name, transaction_name, by_user, transaction_date) VALUES('CUSTOMERS', l_transaction, USER, SYSDATE); END; /