SlideShare a Scribd company logo
1 of 12
1 Using ORACLE® Triggers and Cursors
2 Triggers A trigger is a stored procedure that defines an action that the database automatically initiates when some database related event such as INSERT , UPDATE or DELETE occurs. 					          when occurs fires a  The main difference between a procedure and a trigger is that a procedure is executed explicitly from another block via a procedure call with arguments, while a trigger is executed implicitly when the triggering event like the occurrence of a DML-statement as INSERT ,UPDATE or DELETE occurs. Also a trigger does not accept arguments. The Automatic execution of triggers is known as firing of trigger.
3 Need and Types of Triggers Need for triggers: Maintaining complex integrity constraints. Auditing the information in a table by recording the change. Automatically signaling another program about an occurred event. Enforcing complex business rules.
4 Need and Types of Triggers Types of triggers: Row trigger: 	Fired once for every row that is affected. Statement trigger : Fired only once per statement regardless of the number of row 			affected. Before trigger:	Fired before the triggering event occurs. After trigger: 	Fired after the triggering event occurs.
5 Syntax for Triggers SYNTAX : CREATE OR REPLACE TRIGGER trigger_name 	{BEFORE / AFTER }  	{ INSERT / UPDATE / DELETE} ON object_name 	FOR { EACH ROW / EACH STATEMENT} BEGIN 	Statement1… 	….. 	…... END;
6 Example for triggers SYNTAX : CREATE OR REPLACE TRIGGER trig 	AFTER DELETE ON InfoTable 	FOR EACH ROW BEGIN 	INSERT INTO InfoTable VALUES(:old name,NULL,NULL); END; 	Using this trigger , whenever a row from the InfoTable will be deleted the trigger will be fired and the row containing the deleted row’s name will be inserted with other values as NULL which will help us identify later which rows were deleted.
7 NEED FOR CURSORS We cannot use sub-programs of PL/SQL with a simple select statement to retrieve more than one row. If a select statement in a procedure returns more than one row ,Oracle returns an error message since PL/SQL requires a special compatibility to retrieve and process more than one row. 			Oracle processes procedure without error 			Oracle returns error. SELECT statement in procedure returning one row row SELECT statement in procedure returning more than one row row row row
8 CURSOR A PL/SQL cursor is a mechanism that provides a way to select multiple rows of data from the database and then process each row individually inside a PL/SQL program. A cursor is basically an area of memory containing SQL statements and information for processing those statements. A CURSOR is a pointer to the context area. 		CURSOR is 			 		pointing at  		this row1				             CURSOR is  							             pointing at 						             this row2 now 	   The cursor first points at row1 and once it is processed it then advances to row2 	    and so on. row1 row1 row2 row2 row3 row3 row4 row4
9 Types of CURSORS There are two types of cursors: Implicit cursor: Created and used by Oracle for all DML and PL/SQL select statements including those returning only one row. Explicit cursor : Created and used by the programmer for queries that return more than one row.
10 Types of CURSORS SYNTAX: DECLARE CURSOR cursor_name   AS					Cursor Declaration SELECT statement; --  Variable declaration if any; BEGIN OPEN cursor_name; FETCH <cursor_name> INTO <record_list>	 Cursor Body -- data fetched into active data set. CLOSE cursor_name; END;
11 EXAMPLE OF CURSOR DECLARE CURSOR cus AS SELECT * FROM InfoTable; customer  InfoTable%ROWTYPE;	         declare a rowtype variable BEGIN OPEN cus; LOOP FETCH cus into customer;		         Fetch the row into the variable and advance cursor. DBMS_OUTPUT.PUT_LINE(‘name:’||customer.name); DBMS_OUTPUT.PUT_LINE(‘age:’||customer.age); DBMS_OUTPUT.PUT_LINE(‘phone:’||customer.phone); EXIT WHEN cus%NOTFOUND;	        Returns true when all rows have been fetched. END LOOP; CLOSE cus; END;
THANK YOU 12 THANK YOU FOR VIEWING THIS PRESENTATION FOR MORE PRESENTATIONS AND VIDEOS ON ORACLE AND DATAMINING , please visit:   www.dataminingtools.net

More Related Content

What's hot

Lecture02 abap on line
Lecture02 abap on lineLecture02 abap on line
Lecture02 abap on line
Milind Patil
 
Triggers
TriggersTriggers
Triggers
work
 

What's hot (18)

10053 - null is not nothing
10053 - null is not nothing10053 - null is not nothing
10053 - null is not nothing
 
Mysql creating stored function
Mysql  creating stored function Mysql  creating stored function
Mysql creating stored function
 
Not in vs not exists
Not in vs not existsNot in vs not exists
Not in vs not exists
 
Introduction to triggers
Introduction to triggersIntroduction to triggers
Introduction to triggers
 
PL/SQL - CURSORS
PL/SQL - CURSORSPL/SQL - CURSORS
PL/SQL - CURSORS
 
8
88
8
 
Lecture02 abap on line
Lecture02 abap on lineLecture02 abap on line
Lecture02 abap on line
 
Introduction to triggers
Introduction to triggersIntroduction to triggers
Introduction to triggers
 
PLSQL Advanced
PLSQL AdvancedPLSQL Advanced
PLSQL Advanced
 
Stored procedure in sql server
Stored procedure in sql serverStored procedure in sql server
Stored procedure in sql server
 
Oracle: PLSQL Introduction
Oracle: PLSQL IntroductionOracle: PLSQL Introduction
Oracle: PLSQL Introduction
 
Assignment
AssignmentAssignment
Assignment
 
Oracle Database Trigger
Oracle Database TriggerOracle Database Trigger
Oracle Database Trigger
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Triggers
TriggersTriggers
Triggers
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
 
Ado.net by Awais Majeed
Ado.net by Awais MajeedAdo.net by Awais Majeed
Ado.net by Awais Majeed
 

Viewers also liked

Actividad 1 un dia en la vida de
Actividad 1     un dia en la vida deActividad 1     un dia en la vida de
Actividad 1 un dia en la vida de
Guillermo Mondragon
 
Building and Managing Social Media Collections
Building and Managing Social Media CollectionsBuilding and Managing Social Media Collections
Building and Managing Social Media Collections
Jason Casden
 
Modelo do dossiê 2014 - prêmio escola de qualidade
Modelo do dossiê  2014 - prêmio escola de qualidadeModelo do dossiê  2014 - prêmio escola de qualidade
Modelo do dossiê 2014 - prêmio escola de qualidade
em_raimundofernandes
 

Viewers also liked (13)

Del rosario
Del rosarioDel rosario
Del rosario
 
Receitas PráTicas
Receitas PráTicasReceitas PráTicas
Receitas PráTicas
 
linkinding
linkindinglinkinding
linkinding
 
Actividad 1 un dia en la vida de
Actividad 1     un dia en la vida deActividad 1     un dia en la vida de
Actividad 1 un dia en la vida de
 
(아울러) 빅데이터실록 By 박한우
(아울러) 빅데이터실록 By 박한우(아울러) 빅데이터실록 By 박한우
(아울러) 빅데이터실록 By 박한우
 
Dennis yu social amplification engine_guide_v5.1_2016_1210
Dennis yu social amplification engine_guide_v5.1_2016_1210Dennis yu social amplification engine_guide_v5.1_2016_1210
Dennis yu social amplification engine_guide_v5.1_2016_1210
 
Mangalyaan
MangalyaanMangalyaan
Mangalyaan
 
Building and Managing Social Media Collections
Building and Managing Social Media CollectionsBuilding and Managing Social Media Collections
Building and Managing Social Media Collections
 
Modelo do dossiê 2014 - prêmio escola de qualidade
Modelo do dossiê  2014 - prêmio escola de qualidadeModelo do dossiê  2014 - prêmio escola de qualidade
Modelo do dossiê 2014 - prêmio escola de qualidade
 
Forgive and forget
Forgive and forgetForgive and forget
Forgive and forget
 
Oracle: Programs
Oracle: ProgramsOracle: Programs
Oracle: Programs
 
Marketing management of mushroom and 7 p’s A Presentation By Mr Allah Dad k...
Marketing management  of mushroom and 7 p’s  A Presentation By Mr Allah Dad k...Marketing management  of mushroom and 7 p’s  A Presentation By Mr Allah Dad k...
Marketing management of mushroom and 7 p’s A Presentation By Mr Allah Dad k...
 
Oracle: Joins
Oracle: JoinsOracle: Joins
Oracle: Joins
 

Similar to Oracle:Cursors

3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
Ashwin Kumar
 
plsql tutorialhub....
plsql tutorialhub....plsql tutorialhub....
plsql tutorialhub....
Abhiram Vijay
 
11303 dbms chap_02_triggers (2)
11303 dbms chap_02_triggers (2)11303 dbms chap_02_triggers (2)
11303 dbms chap_02_triggers (2)
Simarjit Mann
 
Intro to tsql unit 13
Intro to tsql   unit 13Intro to tsql   unit 13
Intro to tsql unit 13
Syed Asrarali
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programming
Rushdi Shams
 
My cool new Slideshow!
My cool new Slideshow!My cool new Slideshow!
My cool new Slideshow!
Anas Mohammed
 

Similar to Oracle:Cursors (20)

3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
 
SQL / PL
SQL / PLSQL / PL
SQL / PL
 
plsql tutorialhub....
plsql tutorialhub....plsql tutorialhub....
plsql tutorialhub....
 
Triggers n Cursors.ppt
Triggers n Cursors.pptTriggers n Cursors.ppt
Triggers n Cursors.ppt
 
Cursors
CursorsCursors
Cursors
 
Oracle PL/SQL online training | PL/SQL online Training
Oracle PL/SQL online training | PL/SQL online TrainingOracle PL/SQL online training | PL/SQL online Training
Oracle PL/SQL online training | PL/SQL online Training
 
11303 dbms chap_02_triggers (2)
11303 dbms chap_02_triggers (2)11303 dbms chap_02_triggers (2)
11303 dbms chap_02_triggers (2)
 
4 cursors
4 cursors4 cursors
4 cursors
 
PLSQL (1).ppt
PLSQL (1).pptPLSQL (1).ppt
PLSQL (1).ppt
 
PLSQL CURSOR
PLSQL CURSORPLSQL CURSOR
PLSQL CURSOR
 
Intro to tsql unit 13
Intro to tsql   unit 13Intro to tsql   unit 13
Intro to tsql unit 13
 
Cursors.ppt
Cursors.pptCursors.ppt
Cursors.ppt
 
PLSQL.pptx
PLSQL.pptxPLSQL.pptx
PLSQL.pptx
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programming
 
Cursors
CursorsCursors
Cursors
 
My cool new Slideshow!
My cool new Slideshow!My cool new Slideshow!
My cool new Slideshow!
 
PLSQL Note
PLSQL NotePLSQL Note
PLSQL Note
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 
Dynamic websites lec3
Dynamic websites lec3Dynamic websites lec3
Dynamic websites lec3
 

More from oracle content (10)

Oracle: PLSQL Introduction
Oracle: PLSQL IntroductionOracle: PLSQL Introduction
Oracle: PLSQL Introduction
 
Oracle : DML
Oracle : DMLOracle : DML
Oracle : DML
 
Oracle: Commands
Oracle: CommandsOracle: Commands
Oracle: Commands
 
Oracle: Control Structures
Oracle:  Control StructuresOracle:  Control Structures
Oracle: Control Structures
 
Oracle: Dw Design
Oracle: Dw DesignOracle: Dw Design
Oracle: Dw Design
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
 
Oracle Warehouse
Oracle WarehouseOracle Warehouse
Oracle Warehouse
 
Oracle: Functions
Oracle: FunctionsOracle: Functions
Oracle: Functions
 
Oracle: New Plsql
Oracle: New PlsqlOracle: New Plsql
Oracle: New Plsql
 
Oracle: Fundamental Of Dw
Oracle: Fundamental Of DwOracle: Fundamental Of Dw
Oracle: Fundamental Of Dw
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

Oracle:Cursors

  • 1. 1 Using ORACLE® Triggers and Cursors
  • 2. 2 Triggers A trigger is a stored procedure that defines an action that the database automatically initiates when some database related event such as INSERT , UPDATE or DELETE occurs. when occurs fires a The main difference between a procedure and a trigger is that a procedure is executed explicitly from another block via a procedure call with arguments, while a trigger is executed implicitly when the triggering event like the occurrence of a DML-statement as INSERT ,UPDATE or DELETE occurs. Also a trigger does not accept arguments. The Automatic execution of triggers is known as firing of trigger.
  • 3. 3 Need and Types of Triggers Need for triggers: Maintaining complex integrity constraints. Auditing the information in a table by recording the change. Automatically signaling another program about an occurred event. Enforcing complex business rules.
  • 4. 4 Need and Types of Triggers Types of triggers: Row trigger: Fired once for every row that is affected. Statement trigger : Fired only once per statement regardless of the number of row affected. Before trigger: Fired before the triggering event occurs. After trigger: Fired after the triggering event occurs.
  • 5. 5 Syntax for Triggers SYNTAX : CREATE OR REPLACE TRIGGER trigger_name {BEFORE / AFTER } { INSERT / UPDATE / DELETE} ON object_name FOR { EACH ROW / EACH STATEMENT} BEGIN Statement1… ….. …... END;
  • 6. 6 Example for triggers SYNTAX : CREATE OR REPLACE TRIGGER trig AFTER DELETE ON InfoTable FOR EACH ROW BEGIN INSERT INTO InfoTable VALUES(:old name,NULL,NULL); END; Using this trigger , whenever a row from the InfoTable will be deleted the trigger will be fired and the row containing the deleted row’s name will be inserted with other values as NULL which will help us identify later which rows were deleted.
  • 7. 7 NEED FOR CURSORS We cannot use sub-programs of PL/SQL with a simple select statement to retrieve more than one row. If a select statement in a procedure returns more than one row ,Oracle returns an error message since PL/SQL requires a special compatibility to retrieve and process more than one row. Oracle processes procedure without error Oracle returns error. SELECT statement in procedure returning one row row SELECT statement in procedure returning more than one row row row row
  • 8. 8 CURSOR A PL/SQL cursor is a mechanism that provides a way to select multiple rows of data from the database and then process each row individually inside a PL/SQL program. A cursor is basically an area of memory containing SQL statements and information for processing those statements. A CURSOR is a pointer to the context area. CURSOR is pointing at this row1 CURSOR is pointing at this row2 now The cursor first points at row1 and once it is processed it then advances to row2 and so on. row1 row1 row2 row2 row3 row3 row4 row4
  • 9. 9 Types of CURSORS There are two types of cursors: Implicit cursor: Created and used by Oracle for all DML and PL/SQL select statements including those returning only one row. Explicit cursor : Created and used by the programmer for queries that return more than one row.
  • 10. 10 Types of CURSORS SYNTAX: DECLARE CURSOR cursor_name AS Cursor Declaration SELECT statement; -- Variable declaration if any; BEGIN OPEN cursor_name; FETCH <cursor_name> INTO <record_list> Cursor Body -- data fetched into active data set. CLOSE cursor_name; END;
  • 11. 11 EXAMPLE OF CURSOR DECLARE CURSOR cus AS SELECT * FROM InfoTable; customer InfoTable%ROWTYPE; declare a rowtype variable BEGIN OPEN cus; LOOP FETCH cus into customer; Fetch the row into the variable and advance cursor. DBMS_OUTPUT.PUT_LINE(‘name:’||customer.name); DBMS_OUTPUT.PUT_LINE(‘age:’||customer.age); DBMS_OUTPUT.PUT_LINE(‘phone:’||customer.phone); EXIT WHEN cus%NOTFOUND; Returns true when all rows have been fetched. END LOOP; CLOSE cus; END;
  • 12. THANK YOU 12 THANK YOU FOR VIEWING THIS PRESENTATION FOR MORE PRESENTATIONS AND VIDEOS ON ORACLE AND DATAMINING , please visit: www.dataminingtools.net