SlideShare a Scribd company logo
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;
Ad

More Related Content

What's hot (20)

Dbms 14: Relational Calculus
Dbms 14: Relational CalculusDbms 14: Relational Calculus
Dbms 14: Relational Calculus
Amiya9439793168
 
K-Nearest Neighbor Classifier
K-Nearest Neighbor ClassifierK-Nearest Neighbor Classifier
K-Nearest Neighbor Classifier
Neha Kulkarni
 
Oracle database introduction
Oracle database introductionOracle database introduction
Oracle database introduction
Mohammad Javad Beheshtian
 
Data modeling case study
Data modeling case studyData modeling case study
Data modeling case study
minderchen
 
Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2
Iffat Anjum
 
PL/SQL - CURSORS
PL/SQL - CURSORSPL/SQL - CURSORS
PL/SQL - CURSORS
IshaRana14
 
Data Warehousing and Data Mining
Data Warehousing and Data MiningData Warehousing and Data Mining
Data Warehousing and Data Mining
idnats
 
10. XML in DBMS
10. XML in DBMS10. XML in DBMS
10. XML in DBMS
koolkampus
 
TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....
TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....
TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....
Shail Nakum
 
Normalization in DBMS
Normalization in DBMSNormalization in DBMS
Normalization in DBMS
Hitesh Mohapatra
 
Measures of query cost
Measures of query costMeasures of query cost
Measures of query cost
Hitesh Mohapatra
 
Data Analysis: Evaluation Metrics for Supervised Learning Models of Machine L...
Data Analysis: Evaluation Metrics for Supervised Learning Models of Machine L...Data Analysis: Evaluation Metrics for Supervised Learning Models of Machine L...
Data Analysis: Evaluation Metrics for Supervised Learning Models of Machine L...
Md. Main Uddin Rony
 
Adbms 17 object query language
Adbms 17 object query languageAdbms 17 object query language
Adbms 17 object query language
Vaibhav Khanna
 
Object oriented databases
Object oriented databasesObject oriented databases
Object oriented databases
Sajith Ekanayaka
 
Lecture4 big data technology foundations
Lecture4 big data technology foundationsLecture4 big data technology foundations
Lecture4 big data technology foundations
hktripathy
 
Er model ppt
Er model pptEr model ppt
Er model ppt
Pihu Goel
 
Logical and shift micro operations
Logical and shift micro operationsLogical and shift micro operations
Logical and shift micro operations
Sanjeev Patel
 
Data science life cycle
Data science life cycleData science life cycle
Data science life cycle
Manoj Mishra
 
Data Models.ppt
Data Models.pptData Models.ppt
Data Models.ppt
AnshikaGoel42
 
Difference between molap, rolap and holap in ssas
Difference between molap, rolap and holap  in ssasDifference between molap, rolap and holap  in ssas
Difference between molap, rolap and holap in ssas
Umar Ali
 
Dbms 14: Relational Calculus
Dbms 14: Relational CalculusDbms 14: Relational Calculus
Dbms 14: Relational Calculus
Amiya9439793168
 
K-Nearest Neighbor Classifier
K-Nearest Neighbor ClassifierK-Nearest Neighbor Classifier
K-Nearest Neighbor Classifier
Neha Kulkarni
 
Data modeling case study
Data modeling case studyData modeling case study
Data modeling case study
minderchen
 
Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2
Iffat Anjum
 
PL/SQL - CURSORS
PL/SQL - CURSORSPL/SQL - CURSORS
PL/SQL - CURSORS
IshaRana14
 
Data Warehousing and Data Mining
Data Warehousing and Data MiningData Warehousing and Data Mining
Data Warehousing and Data Mining
idnats
 
10. XML in DBMS
10. XML in DBMS10. XML in DBMS
10. XML in DBMS
koolkampus
 
TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....
TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....
TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....
Shail Nakum
 
Data Analysis: Evaluation Metrics for Supervised Learning Models of Machine L...
Data Analysis: Evaluation Metrics for Supervised Learning Models of Machine L...Data Analysis: Evaluation Metrics for Supervised Learning Models of Machine L...
Data Analysis: Evaluation Metrics for Supervised Learning Models of Machine L...
Md. Main Uddin Rony
 
Adbms 17 object query language
Adbms 17 object query languageAdbms 17 object query language
Adbms 17 object query language
Vaibhav Khanna
 
Lecture4 big data technology foundations
Lecture4 big data technology foundationsLecture4 big data technology foundations
Lecture4 big data technology foundations
hktripathy
 
Er model ppt
Er model pptEr model ppt
Er model ppt
Pihu Goel
 
Logical and shift micro operations
Logical and shift micro operationsLogical and shift micro operations
Logical and shift micro operations
Sanjeev Patel
 
Data science life cycle
Data science life cycleData science life cycle
Data science life cycle
Manoj Mishra
 
Difference between molap, rolap and holap in ssas
Difference between molap, rolap and holap  in ssasDifference between molap, rolap and holap  in ssas
Difference between molap, rolap and holap in ssas
Umar Ali
 

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
suresh
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
Rumman Ansari
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
Ashwin Kumar
 
PROCEDURAL LANGUAGE/ STRUCTURED QUERY LANGUAGE.pdf
PROCEDURAL LANGUAGE/ STRUCTURED QUERY LANGUAGE.pdfPROCEDURAL LANGUAGE/ STRUCTURED QUERY LANGUAGE.pdf
PROCEDURAL LANGUAGE/ STRUCTURED QUERY LANGUAGE.pdf
rajeswaria21
 
SQL OVERVIEW for a new introduced student.pptx
SQL OVERVIEW for a new introduced student.pptxSQL OVERVIEW for a new introduced student.pptx
SQL OVERVIEW for a new introduced student.pptx
JosephNhlane
 
Oracle etl openworld
Oracle etl openworldOracle etl openworld
Oracle etl openworld
Rodrigo Bastos
 
PL/SQL is a block structured language that enables developers to combine the ...
PL/SQL is a block structured language that enables developers to combine the ...PL/SQL is a block structured language that enables developers to combine the ...
PL/SQL is a block structured language that enables developers to combine the ...
renuka b
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programming
Rushdi Shams
 
chapter 1.pdfbbbbbbbbbbbbbbbbbbbbbbbbbb
chapter  1.pdfbbbbbbbbbbbbbbbbbbbbbbbbbbchapter  1.pdfbbbbbbbbbbbbbbbbbbbbbbbbbb
chapter 1.pdfbbbbbbbbbbbbbbbbbbbbbbbbbb
hhhhhcccc25
 
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrjPLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
KathanPatel49
 
Introduction to SQL, SQL*Plus
Introduction to SQL, SQL*PlusIntroduction to SQL, SQL*Plus
Introduction to SQL, SQL*Plus
Chhom Karath
 
introduction to SQL query language beginner.ppt
introduction to SQL query language beginner.pptintroduction to SQL query language beginner.ppt
introduction to SQL query language beginner.ppt
PatriceRochon1
 
Sq lite
Sq liteSq lite
Sq lite
Revuru Bharadwaja
 
Erik_van_Roon.pdf
Erik_van_Roon.pdfErik_van_Roon.pdf
Erik_van_Roon.pdf
DetchDuvanGaelaCamar
 
Chapter09
Chapter09Chapter09
Chapter09
sasa_eldoby
 
Pl sql-ch1
Pl sql-ch1Pl sql-ch1
Pl sql-ch1
Mukesh Tekwani
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
prabhu rajendran
 
embedded-static-&dynamic
embedded-static-&dynamicembedded-static-&dynamic
embedded-static-&dynamic
Saranya Natarajan
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
Divyank Jindal
 
Dbms important questions and answers
Dbms important questions and answersDbms important questions and answers
Dbms important questions and answers
LakshmiSarvani6
 
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
suresh
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
Ashwin Kumar
 
PROCEDURAL LANGUAGE/ STRUCTURED QUERY LANGUAGE.pdf
PROCEDURAL LANGUAGE/ STRUCTURED QUERY LANGUAGE.pdfPROCEDURAL LANGUAGE/ STRUCTURED QUERY LANGUAGE.pdf
PROCEDURAL LANGUAGE/ STRUCTURED QUERY LANGUAGE.pdf
rajeswaria21
 
SQL OVERVIEW for a new introduced student.pptx
SQL OVERVIEW for a new introduced student.pptxSQL OVERVIEW for a new introduced student.pptx
SQL OVERVIEW for a new introduced student.pptx
JosephNhlane
 
PL/SQL is a block structured language that enables developers to combine the ...
PL/SQL is a block structured language that enables developers to combine the ...PL/SQL is a block structured language that enables developers to combine the ...
PL/SQL is a block structured language that enables developers to combine the ...
renuka b
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programming
Rushdi Shams
 
chapter 1.pdfbbbbbbbbbbbbbbbbbbbbbbbbbb
chapter  1.pdfbbbbbbbbbbbbbbbbbbbbbbbbbbchapter  1.pdfbbbbbbbbbbbbbbbbbbbbbbbbbb
chapter 1.pdfbbbbbbbbbbbbbbbbbbbbbbbbbb
hhhhhcccc25
 
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrjPLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
KathanPatel49
 
Introduction to SQL, SQL*Plus
Introduction to SQL, SQL*PlusIntroduction to SQL, SQL*Plus
Introduction to SQL, SQL*Plus
Chhom Karath
 
introduction to SQL query language beginner.ppt
introduction to SQL query language beginner.pptintroduction to SQL query language beginner.ppt
introduction to SQL query language beginner.ppt
PatriceRochon1
 
Dbms important questions and answers
Dbms important questions and answersDbms important questions and answers
Dbms important questions and answers
LakshmiSarvani6
 
Ad

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)
Vaibhav Kathuria
 
Copy of sec d (2)
Copy of sec d (2)Copy of sec d (2)
Copy of sec d (2)
Vaibhav Kathuria
 
Database normalization
Database normalizationDatabase normalization
Database normalization
Vaibhav Kathuria
 
Dbms mca-section a
Dbms mca-section aDbms mca-section a
Dbms mca-section a
Vaibhav Kathuria
 
Relational algebra calculus
Relational algebra  calculusRelational algebra  calculus
Relational algebra calculus
Vaibhav Kathuria
 
B & c
B & cB & c
B & c
Vaibhav Kathuria
 
Ad

Recently uploaded (20)

Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 

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;