SlideShare a Scribd company logo
1 of 14
Embedded SQL Statements 
 The term embedded SQL refers to SQL statements placed within an application program. Because it 
houses the SQL statements, the application program is called a host program, and the language in 
which it is written is called the host language. For example, Pro*C/C++ allows you to embed 
certain SQL statements in a C or C++ host program. 
 To manipulate and query Oracle data, you use the INSERT, UPDATE, DELETE, and SELECT 
statements. INSERT adds rows of data to database tables, UPDATE modifies rows, DELETE 
removes unwanted rows, and SELECT retrieves rows that meet your search condition. 
 The powerful SET ROLE statement lets you dynamically manage database privileges. A role is a 
named group of related system and/or object privileges granted to users or other roles. Role 
definitions are stored in the Oracle data dictionary. Your applications can use the SET ROLE 
statement to enable and disable roles as needed. 
 Only SQL statements--not SQL*Plus statements--are valid in an application program. (SQL*Plus 
has additional statements for setting environment parameters, editing, and report formatting.)
Embedded SQL 
 SQL provides a powerful declarative query language. However, access to a database from a 
general-purpose programming language is required because, 
 SQL is not as powerful as a general-purpose programming language. There are queries 
that cannot be expressed in SQL, but can be programmed in C, Fortran, Pascal, Cobol, 
etc. 
 Non declarative actions -- such as printing a report, interacting with a user, or sending 
the result to a GUI -- cannot be done from within SQL. 
 The SQL standard defines embedding of SQL as embedded SQL and the language in which 
SQL queries are embedded is referred as host language. 
 The result of the query is made available to the program one tuple (record) at a time. 
 To identify embedded SQL requests to the preprocessor, we use EXEC SQL statement: 
EXEC SQL embedded SQL statement END-EXEC 
 Embedded SQL can execute any valid update, insert, or delete statements. 
 Dynamic SQL component allows programs to construct and submit SQL queries ar run time
Embedded SQL Syntax 
 In your application program, you can freely intermix complete SQL statements with complete C 
statements and use C variables or structures in SQL statements. The only special requirement for 
building SQL statements into your host program is that you begin them with the keywords 
EXEC SQL and end them with a semicolon. Pro*C/C++ translates all EXEC SQL statements 
into calls to the runtime library SQLLIB. 
 Many embedded SQL statements differ from their interactive counterparts only through the 
addition of a new clause or the use of program variables. The following example compares 
interactive and embedded ROLLBACK statements: 
 ROLLBACK WORK: -- interactive 
 EXEC SQL ROLLBACK WORK; -- embedded 
These statements have the same effect, but you would use the first in an interactive SQL 
environment (such as when running SQL*Plus), and the second in a Pro*C/C++ program.
What is PL/SQL? 
 PL/SQL stands for Procedural Language extension of SQL. 
 PL/SQL is a combination of SQL along with the procedural features of programming 
languages. 
 It was developed by Oracle Corporation in the early 90’s to enhance the capabilities of SQL. 
SQL (Structured Query Language) is used to modify and access data or information from 
a storage area called database. This beginner online training sql tutorial website teaches 
you the basics of SQL code and train you how to write & program SQL queries. I will be 
sharing my database knowledge on SQL and help you learn programming SQL better. The 
concepts discussed in this SQL tutorial can be applied to most of database systems. The SQL 
syntax used to explain the tutorial concepts is similar to the one used in Oracle database.
My SQL Data Base 
In a simple manner, SQL is a non-procedural, English-like language that processes data in 
groups of records rather than one record at a time. Few functions of SQL are: 
 SELECT - extracts data from a database 
 UPDATE - updates data in a database 
 DELETE - deletes data from a database 
 INSERT INTO - inserts new data into a database 
 CREATE DATABASE - creates a new database 
 ALTER DATABASE - modifies a database 
 CREATE TABLE - creates a new table 
 ALTER TABLE - modifies a table 
 DROP TABLE - deletes a table 
 CREATE INDEX - creates an index (search key) 
 DROP INDEX - deletes an index
Advantages of PL/SQL 
These are the Advantages of PL/SQL 
• Block Structures: PL SQL consists of blocks of code, which can be nested within each 
other. Each block forms a unit of a task or a logical module. PL/SQL Blocks can be stored in 
the database and reused. 
• 
Procedural Language Capability: PL SQL consists of procedural language constructs such 
as conditional statements (if else statements) and loops like (FOR loops). 
• 
Better Performance: PL SQL engine processes multiple SQL statements simultaneously as 
a single block, thereby reducing network traffic.
What are Cursors? 
A cursor is a temporary work area created in the system memory when a SQL statement is executed. A 
cursor contains information on a select statement and the rows of data accessed by it. 
This temporary work area is used to store the data retrieved from the database, and manipulate this 
data. A cursor can hold more than one row, but can process only one row at a time. The set of rows the 
cursor holds is called the active set. 
There are two types of cursors in PL/SQL: 
Implicit cursors 
These are created by default when DML statements like, INSERT, UPDATE, and DELETE statements 
are executed. They are also created when a SELECT statement that returns just one row is executed. 
Explicit cursors 
They must be created when you are executing a SELECT statement that returns more than one row. 
Even though the cursor stores multiple records, only one record can be processed at a time, which is 
called as current row. When you fetch a row the current row position moves to next row. 
Both implicit and explicit cursors have the same functionality, but they differ in the way they are 
accessed.
Implicit Cursors: Application 
 When you execute DML statements like DELETE, INSERT, UPDATE and SELECT 
statements, implicit statements are created to process these statements. 
 Oracle provides few attributes called as implicit cursor attributes to check the status of 
DML operations. The cursor attributes available are %FOUND, %NOTFOUND, 
%ROWCOUNT, and %ISOPEN. 
 For example, 
When you execute INSERT, UPDATE, or DELETE statements the cursor attributes tell us 
whether any rows are affected and how many have been affected. 
When a SELECT... INTO statement is executed in a PL/SQL Block, implicit cursor attributes 
can be used to find out whether any row has been returned by the SELECT statement. 
PL/SQL returns an error when no data is selected.
Status of the cursor for each of these attributes are defined in the below table. 
Attributes Return Value Example 
%FOUND The return value is TRUE, if the DML statements 
like INSERT, DELETE and UPDATE affect at least one 
row and if SELECT ….INTO statement return at least 
one row. 
SQL%FOUND 
The return value is FALSE, if DML statements like 
INSERT, DELETE and UPDATE do not affect row and 
if SELECT….INTO statement do not return a row. 
%NOTFOUND The return value is FALSE, if DML statements like 
INSERT, DELETE and UPDATE at least one row and if 
SELECT ….INTO statement return at least one row. 
SQL%NOTFOUND 
The return value is TRUE, if a DML statement like 
INSERT, DELETE and UPDATE do not affect even one 
row and if SELECT ….INTO statement does not 
return a row. 
%ROWCOUNT Return the number of rows affected by the DML 
operations INSERT, DELETE, UPDATE, SELECT 
SQL%ROWCOUNT
What is a Trigger? 
A trigger is a pl/sql block structure which is fired when a DML statements like Insert, Delete, 
Update is executed on a database table. A trigger is triggered automatically when an 
associated DML statement is executed. 
Syntax of Triggers 
Syntax for Creating a Trigger 
CREATE [OR REPLACE ] TRIGGER trigger_name 
{BEFORE | AFTER | INSTEAD OF } 
{INSERT [OR] | UPDATE [OR] | DELETE} 
[OF col_name] 
ON table_name 
[REFERENCING OLD AS o NEW AS n] 
[FOR EACH ROW] 
WHEN (condition) 
BEGIN 
--- sql statements 
END;
 CREATE [OR REPLACE ] TRIGGER trigger_name - This clause creates a trigger with the given name or 
overwrites an existing trigger with the same name. 
 {BEFORE | AFTER | INSTEAD OF } - This clause indicates at what time should the trigger get fired. i.e for 
example: before or after updating a table. INSTEAD OF is used to create a trigger on a view. before and 
after cannot be used to create a trigger on a view. 
 {INSERT [OR] | UPDATE [OR] | DELETE} - This clause determines the triggering event. More than one 
triggering events can be used together separated by OR keyword. The trigger gets fired at all the 
specified triggering event. 
 [OF col_name] - This clause is used with update triggers. This clause is used when you want to trigger an 
event only when a specific column is updated. 
 CREATE [OR REPLACE ] TRIGGER trigger_name - This clause creates a trigger with the given name or 
overwrites an existing trigger with the same name. 
 [ON table_name] - This clause identifies the name of the table or view to which the trigger is associated. 
 [REFERENCING OLD AS o NEW AS n] - This clause is used to reference the old and new values of the data 
being changed. By default, you reference the values as :old.column_name or :new.column_name. The 
reference names can also be changed from old (or new) to any other user-defined name. You cannot 
reference old values when inserting a record, or new values when deleting a record, because they do not 
exist. 
 [FOR EACH ROW] - This clause is used to determine whether a trigger must fire when each row gets 
affected ( i.e. a Row Level Trigger) or just once when the entire sql statement is executed(i.e.statement 
level Trigger) 
 WHEN (condition) - This clause is valid only for row level triggers. The trigger is fired only for rows that 
satisfy the condition specified.
Types of PL/SQL Triggers 
There are two types of triggers based on the which level it is triggered. 
1) Row level trigger - An event is triggered for each row upated, inserted or deleted. 
2) Statement level trigger - An event is triggered for each sql statement executed. 
 PL/SQL Trigger Execution Hierarchy 
 The following hierarchy is followed when a trigger is fired 
1) BEFORE statement trigger fires first. 
2) Next BEFORE row level trigger fires, once for each row affected. 
3) Then AFTER row level trigger fires once for each affected row. This events will 
alternates between BEFORE and AFTER row level triggers. 
4) Finally the AFTER statement level trigger fires.
What is a Stored Procedure? 
A stored procedure or in simple a proc is a named PL/SQL block which performs one or more specific 
task. This is similar to a procedure in other programming languages. 
 A procedure has a header and a body. The header consists of the name of the procedure and the 
parameters or variables passed to the procedure. The body consists or declaration 
section, execution section and exception section similar to a general PL/SQL Block. 
 A procedure is similar to an anonymous PL/SQL Block but it is named for repeated usage. 
We can pass parameters to procedures in three ways. 
1) IN-parameters 
2) OUT-parameters 
3) IN OUT-parameter
General Syntax to create a procedure is: 
CREATE [OR REPLACE] PROCEDURE proc_name [list of parameters] 
IS 
Declaration section 
BEGIN 
Execution section 
EXCEPTION 
Exception section 
END; 
How to execute a Stored Procedure? 
There are two ways to execute a procedure. 
1) From the SQL prompt. 
EXECUTE [or EXEC] procedure_name; 
2) Within another procedure – simply use the procedure name. 
procedure_name;

More Related Content

What's hot

PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts Bharat Kalia
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql ProcedurePooja Dixit
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQLEhsan Hamzei
 
Displaying data from multiple tables
Displaying data from multiple tablesDisplaying data from multiple tables
Displaying data from multiple tablesSyed Zaid Irshad
 
Unit I Database concepts - RDBMS & ORACLE
Unit I  Database concepts - RDBMS & ORACLEUnit I  Database concepts - RDBMS & ORACLE
Unit I Database concepts - RDBMS & ORACLEDrkhanchanaR
 
Constraints In Sql
Constraints In SqlConstraints In Sql
Constraints In SqlAnurag
 
VB.Net-Controls and events
VB.Net-Controls and eventsVB.Net-Controls and events
VB.Net-Controls and eventsPrachi Sasankar
 
Dbms
DbmsDbms
DbmsCAG
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introductionSmriti Jain
 
Database Management System Introduction
Database Management System IntroductionDatabase Management System Introduction
Database Management System IntroductionSmriti Jain
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQLrehaniltifat
 

What's hot (20)

Er diagrams presentation
Er diagrams presentationEr diagrams presentation
Er diagrams presentation
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql Procedure
 
Relational model
Relational modelRelational model
Relational model
 
Normalization in DBMS
Normalization in DBMSNormalization in DBMS
Normalization in DBMS
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
SQL(DDL & DML)
SQL(DDL & DML)SQL(DDL & DML)
SQL(DDL & DML)
 
Sql commands
Sql commandsSql commands
Sql commands
 
DBMS Practical File
DBMS Practical FileDBMS Practical File
DBMS Practical File
 
DBMS Keys
DBMS KeysDBMS Keys
DBMS Keys
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
Displaying data from multiple tables
Displaying data from multiple tablesDisplaying data from multiple tables
Displaying data from multiple tables
 
Unit I Database concepts - RDBMS & ORACLE
Unit I  Database concepts - RDBMS & ORACLEUnit I  Database concepts - RDBMS & ORACLE
Unit I Database concepts - RDBMS & ORACLE
 
Constraints In Sql
Constraints In SqlConstraints In Sql
Constraints In Sql
 
VB.Net-Controls and events
VB.Net-Controls and eventsVB.Net-Controls and events
VB.Net-Controls and events
 
Dbms
DbmsDbms
Dbms
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
Entity relationship modelling
Entity relationship modellingEntity relationship modelling
Entity relationship modelling
 
Database Management System Introduction
Database Management System IntroductionDatabase Management System Introduction
Database Management System Introduction
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
 

Similar to Cursors, triggers, procedures

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 Trainingsuresh
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-onlyAshwin Kumar
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programmingRushdi Shams
 
Introduction to SQL, SQL*Plus
Introduction to SQL, SQL*PlusIntroduction to SQL, SQL*Plus
Introduction to SQL, SQL*PlusChhom Karath
 
Dbms important questions and answers
Dbms important questions and answersDbms important questions and answers
Dbms important questions and answersLakshmiSarvani6
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...Alex Zaballa
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...Alex Zaballa
 
PL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics CoveredPL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics CoveredDanish Mehraj
 
SQL – The Natural Language for Analysis - Oracle - Whitepaper - 2431343
SQL – The Natural Language for Analysis - Oracle - Whitepaper - 2431343SQL – The Natural Language for Analysis - Oracle - Whitepaper - 2431343
SQL – The Natural Language for Analysis - Oracle - Whitepaper - 2431343Edgar Alejandro Villegas
 

Similar to Cursors, triggers, procedures (20)

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
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
 
Oracle etl openworld
Oracle etl openworldOracle etl openworld
Oracle etl openworld
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programming
 
Introduction to SQL, SQL*Plus
Introduction to SQL, SQL*PlusIntroduction to SQL, SQL*Plus
Introduction to SQL, SQL*Plus
 
Sq lite
Sq liteSq lite
Sq lite
 
Erik_van_Roon.pdf
Erik_van_Roon.pdfErik_van_Roon.pdf
Erik_van_Roon.pdf
 
Chapter09
Chapter09Chapter09
Chapter09
 
Pl sql-ch1
Pl sql-ch1Pl sql-ch1
Pl sql-ch1
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
embedded-static-&dynamic
embedded-static-&dynamicembedded-static-&dynamic
embedded-static-&dynamic
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
Dbms important questions and answers
Dbms important questions and answersDbms important questions and answers
Dbms important questions and answers
 
SQL Query
SQL QuerySQL Query
SQL Query
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
 
PL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics CoveredPL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics Covered
 
SQL – The Natural Language for Analysis - Oracle - Whitepaper - 2431343
SQL – The Natural Language for Analysis - Oracle - Whitepaper - 2431343SQL – The Natural Language for Analysis - Oracle - Whitepaper - 2431343
SQL – The Natural Language for Analysis - Oracle - Whitepaper - 2431343
 
lovely
lovelylovely
lovely
 

More from Vaibhav Kathuria

More from Vaibhav Kathuria (6)

Copy of sec d (2)
Copy of sec d (2)Copy of sec d (2)
Copy of sec d (2)
 
Copy of sec d (2)
Copy of sec d (2)Copy of sec d (2)
Copy of sec d (2)
 
Database normalization
Database normalizationDatabase normalization
Database normalization
 
Dbms mca-section a
Dbms mca-section aDbms mca-section a
Dbms mca-section a
 
Relational algebra calculus
Relational algebra  calculusRelational algebra  calculus
Relational algebra calculus
 
B & c
B & cB & c
B & c
 

Recently uploaded

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 DevelopmentsTrustArc
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
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?Antenna Manufacturer Coco
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
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...Miguel Araújo
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
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.pdfEnterprise Knowledge
 

Recently uploaded (20)

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
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
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?
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
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...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
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
 

Cursors, triggers, procedures

  • 1. Embedded SQL Statements  The term embedded SQL refers to SQL statements placed within an application program. Because it houses the SQL statements, the application program is called a host program, and the language in which it is written is called the host language. For example, Pro*C/C++ allows you to embed certain SQL statements in a C or C++ host program.  To manipulate and query Oracle data, you use the INSERT, UPDATE, DELETE, and SELECT statements. INSERT adds rows of data to database tables, UPDATE modifies rows, DELETE removes unwanted rows, and SELECT retrieves rows that meet your search condition.  The powerful SET ROLE statement lets you dynamically manage database privileges. A role is a named group of related system and/or object privileges granted to users or other roles. Role definitions are stored in the Oracle data dictionary. Your applications can use the SET ROLE statement to enable and disable roles as needed.  Only SQL statements--not SQL*Plus statements--are valid in an application program. (SQL*Plus has additional statements for setting environment parameters, editing, and report formatting.)
  • 2. Embedded SQL  SQL provides a powerful declarative query language. However, access to a database from a general-purpose programming language is required because,  SQL is not as powerful as a general-purpose programming language. There are queries that cannot be expressed in SQL, but can be programmed in C, Fortran, Pascal, Cobol, etc.  Non declarative actions -- such as printing a report, interacting with a user, or sending the result to a GUI -- cannot be done from within SQL.  The SQL standard defines embedding of SQL as embedded SQL and the language in which SQL queries are embedded is referred as host language.  The result of the query is made available to the program one tuple (record) at a time.  To identify embedded SQL requests to the preprocessor, we use EXEC SQL statement: EXEC SQL embedded SQL statement END-EXEC  Embedded SQL can execute any valid update, insert, or delete statements.  Dynamic SQL component allows programs to construct and submit SQL queries ar run time
  • 3. Embedded SQL Syntax  In your application program, you can freely intermix complete SQL statements with complete C statements and use C variables or structures in SQL statements. The only special requirement for building SQL statements into your host program is that you begin them with the keywords EXEC SQL and end them with a semicolon. Pro*C/C++ translates all EXEC SQL statements into calls to the runtime library SQLLIB.  Many embedded SQL statements differ from their interactive counterparts only through the addition of a new clause or the use of program variables. The following example compares interactive and embedded ROLLBACK statements:  ROLLBACK WORK: -- interactive  EXEC SQL ROLLBACK WORK; -- embedded These statements have the same effect, but you would use the first in an interactive SQL environment (such as when running SQL*Plus), and the second in a Pro*C/C++ program.
  • 4. What is PL/SQL?  PL/SQL stands for Procedural Language extension of SQL.  PL/SQL is a combination of SQL along with the procedural features of programming languages.  It was developed by Oracle Corporation in the early 90’s to enhance the capabilities of SQL. SQL (Structured Query Language) is used to modify and access data or information from a storage area called database. This beginner online training sql tutorial website teaches you the basics of SQL code and train you how to write & program SQL queries. I will be sharing my database knowledge on SQL and help you learn programming SQL better. The concepts discussed in this SQL tutorial can be applied to most of database systems. The SQL syntax used to explain the tutorial concepts is similar to the one used in Oracle database.
  • 5. My SQL Data Base In a simple manner, SQL is a non-procedural, English-like language that processes data in groups of records rather than one record at a time. Few functions of SQL are:  SELECT - extracts data from a database  UPDATE - updates data in a database  DELETE - deletes data from a database  INSERT INTO - inserts new data into a database  CREATE DATABASE - creates a new database  ALTER DATABASE - modifies a database  CREATE TABLE - creates a new table  ALTER TABLE - modifies a table  DROP TABLE - deletes a table  CREATE INDEX - creates an index (search key)  DROP INDEX - deletes an index
  • 6. Advantages of PL/SQL These are the Advantages of PL/SQL • Block Structures: PL SQL consists of blocks of code, which can be nested within each other. Each block forms a unit of a task or a logical module. PL/SQL Blocks can be stored in the database and reused. • Procedural Language Capability: PL SQL consists of procedural language constructs such as conditional statements (if else statements) and loops like (FOR loops). • Better Performance: PL SQL engine processes multiple SQL statements simultaneously as a single block, thereby reducing network traffic.
  • 7. What are Cursors? A cursor is a temporary work area created in the system memory when a SQL statement is executed. A cursor contains information on a select statement and the rows of data accessed by it. This temporary work area is used to store the data retrieved from the database, and manipulate this data. A cursor can hold more than one row, but can process only one row at a time. The set of rows the cursor holds is called the active set. There are two types of cursors in PL/SQL: Implicit cursors These are created by default when DML statements like, INSERT, UPDATE, and DELETE statements are executed. They are also created when a SELECT statement that returns just one row is executed. Explicit cursors They must be created when you are executing a SELECT statement that returns more than one row. Even though the cursor stores multiple records, only one record can be processed at a time, which is called as current row. When you fetch a row the current row position moves to next row. Both implicit and explicit cursors have the same functionality, but they differ in the way they are accessed.
  • 8. Implicit Cursors: Application  When you execute DML statements like DELETE, INSERT, UPDATE and SELECT statements, implicit statements are created to process these statements.  Oracle provides few attributes called as implicit cursor attributes to check the status of DML operations. The cursor attributes available are %FOUND, %NOTFOUND, %ROWCOUNT, and %ISOPEN.  For example, When you execute INSERT, UPDATE, or DELETE statements the cursor attributes tell us whether any rows are affected and how many have been affected. When a SELECT... INTO statement is executed in a PL/SQL Block, implicit cursor attributes can be used to find out whether any row has been returned by the SELECT statement. PL/SQL returns an error when no data is selected.
  • 9. Status of the cursor for each of these attributes are defined in the below table. Attributes Return Value Example %FOUND The return value is TRUE, if the DML statements like INSERT, DELETE and UPDATE affect at least one row and if SELECT ….INTO statement return at least one row. SQL%FOUND The return value is FALSE, if DML statements like INSERT, DELETE and UPDATE do not affect row and if SELECT….INTO statement do not return a row. %NOTFOUND The return value is FALSE, if DML statements like INSERT, DELETE and UPDATE at least one row and if SELECT ….INTO statement return at least one row. SQL%NOTFOUND The return value is TRUE, if a DML statement like INSERT, DELETE and UPDATE do not affect even one row and if SELECT ….INTO statement does not return a row. %ROWCOUNT Return the number of rows affected by the DML operations INSERT, DELETE, UPDATE, SELECT SQL%ROWCOUNT
  • 10. What is a Trigger? A trigger is a pl/sql block structure which is fired when a DML statements like Insert, Delete, Update is executed on a database table. A trigger is triggered automatically when an associated DML statement is executed. Syntax of Triggers Syntax for Creating a Trigger CREATE [OR REPLACE ] TRIGGER trigger_name {BEFORE | AFTER | INSTEAD OF } {INSERT [OR] | UPDATE [OR] | DELETE} [OF col_name] ON table_name [REFERENCING OLD AS o NEW AS n] [FOR EACH ROW] WHEN (condition) BEGIN --- sql statements END;
  • 11.  CREATE [OR REPLACE ] TRIGGER trigger_name - This clause creates a trigger with the given name or overwrites an existing trigger with the same name.  {BEFORE | AFTER | INSTEAD OF } - This clause indicates at what time should the trigger get fired. i.e for example: before or after updating a table. INSTEAD OF is used to create a trigger on a view. before and after cannot be used to create a trigger on a view.  {INSERT [OR] | UPDATE [OR] | DELETE} - This clause determines the triggering event. More than one triggering events can be used together separated by OR keyword. The trigger gets fired at all the specified triggering event.  [OF col_name] - This clause is used with update triggers. This clause is used when you want to trigger an event only when a specific column is updated.  CREATE [OR REPLACE ] TRIGGER trigger_name - This clause creates a trigger with the given name or overwrites an existing trigger with the same name.  [ON table_name] - This clause identifies the name of the table or view to which the trigger is associated.  [REFERENCING OLD AS o NEW AS n] - This clause is used to reference the old and new values of the data being changed. By default, you reference the values as :old.column_name or :new.column_name. The reference names can also be changed from old (or new) to any other user-defined name. You cannot reference old values when inserting a record, or new values when deleting a record, because they do not exist.  [FOR EACH ROW] - This clause is used to determine whether a trigger must fire when each row gets affected ( i.e. a Row Level Trigger) or just once when the entire sql statement is executed(i.e.statement level Trigger)  WHEN (condition) - This clause is valid only for row level triggers. The trigger is fired only for rows that satisfy the condition specified.
  • 12. Types of PL/SQL Triggers There are two types of triggers based on the which level it is triggered. 1) Row level trigger - An event is triggered for each row upated, inserted or deleted. 2) Statement level trigger - An event is triggered for each sql statement executed.  PL/SQL Trigger Execution Hierarchy  The following hierarchy is followed when a trigger is fired 1) BEFORE statement trigger fires first. 2) Next BEFORE row level trigger fires, once for each row affected. 3) Then AFTER row level trigger fires once for each affected row. This events will alternates between BEFORE and AFTER row level triggers. 4) Finally the AFTER statement level trigger fires.
  • 13. What is a Stored Procedure? A stored procedure or in simple a proc is a named PL/SQL block which performs one or more specific task. This is similar to a procedure in other programming languages.  A procedure has a header and a body. The header consists of the name of the procedure and the parameters or variables passed to the procedure. The body consists or declaration section, execution section and exception section similar to a general PL/SQL Block.  A procedure is similar to an anonymous PL/SQL Block but it is named for repeated usage. We can pass parameters to procedures in three ways. 1) IN-parameters 2) OUT-parameters 3) IN OUT-parameter
  • 14. General Syntax to create a procedure is: CREATE [OR REPLACE] PROCEDURE proc_name [list of parameters] IS Declaration section BEGIN Execution section EXCEPTION Exception section END; How to execute a Stored Procedure? There are two ways to execute a procedure. 1) From the SQL prompt. EXECUTE [or EXEC] procedure_name; 2) Within another procedure – simply use the procedure name. procedure_name;