SlideShare a Scribd company logo
PL / SQL 
By, 
C.Janani, AP / IT
Definition 
• PL/SQL stands for Procedural Language extension of 
SQL. 
• Procedural features of programming languages. 
• Developed by Oracle Corporation in the early 90’s to 
enhance the capabilities of SQL. 
• PL/SQL Block consists of three sections: 
 The Declaration section (optional). 
 The Execution section (mandatory). 
 The Exception (or Error) Handling section (optional). 
DECLARE 
Variable declaration 
BEGIN 
Program Execution 
EXCEPTION 
Exception handling 
END;
Advantages of PL/SQL 
• Block Structures 
• Procedural language Capability 
• Better Performance 
• Error Handling
Conditional Statements in PL/SQL 
IF THEN ELSE STATEMENT 
1) IF condition 
THEN 
statement 1; 
ELSE 
statement 2; 
END IF; 
2) IF condition 1 
THEN 
statement 1; statement 2; 
ELSIF condtion2 
THEN 
statement 3; 
ELSE 
statement 4; 
END IF 
3) IF condition 1 
THEN 
statement 1; 
statement 2; 
ELSIF condtion2 
THEN 
statement 3; 
ELSE statement 4; 
END IF; 
4) IF condition1 
THEN 
ELSE IF condition2 THEN statement1; 
END IF; 
ELSIF condition3 
THEN 
statement2; 
END IF;
Iterative Statements in PL/SQL 
• Simple Loop 
• While Loop 
• For Loop 
LOOP statements; EXIT; 
{or EXIT WHEN 
condition;} 
END LOOP; 
WHILE <condition> 
LOOP statements; END 
LOOP; 
FOR counter IN 
val1..val2 LOOP 
statements; END LOOP;
Cursors 
• A temporary work area created in the system memory 
when a SQL statement is executed. 
• Contains information on a select statement and the 
rows of data accessed by it. 
• Can open a cursor, and repeatedly fetch a tuple then 
move the cursor, until all tuples have been retrieved. 
– Can use the ORDER BY clause, in queries that are accessed 
through a cursor, to control the order in which tuples are 
returned. 
• Fields in ORDER BY clause must also appear in SELECT clause. 
• Can also modify/delete tuple pointed to by a cursor.
SQL / PL
SQL / PL
Output:
Cursor Types: 
• 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.
Stored Procedures 
• 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. 
• Has a header and a body. 
• 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-parameters 
• A procedure may or may not return any value.
Syntax: 
• CREATE [OR REPLACE] PROCEDURE proc_name 
[list of parameters] 
IS 
Declaration section 
BEGIN 
Execution section 
EXCEPTION 
Exception section 
END;
PL/SQL Functions 
• The major difference between a procedure and a function is, a function must 
always return a value, but a procedure may or may not return a value. 
• General Syntax to create a function is 
CREATE [OR REPLACE] FUNCTION function_name [parameters] 
RETURN return_datatype; 
IS 
Declaration_section 
BEGIN 
Execution_section 
Return return_variable; 
EXCEPTION 
exception section 
Return 
return_variable; 
END;
Parameters in Procedure and Functions 
1) IN type parameter: These types of parameters 
are used to send values to stored procedures. 
CREATE [OR REPLACE] PROCEDURE procedure_name ( param_name1 IN 
datatype, param_name12 IN datatype ... ) 
2) OUT type parameter: These types of 
parameters are used to get values from stored 
procedures. This is similar to a return type in 
functions. 
CREATE [OR REPLACE] PROCEDURE proc2 (param_name OUT datatype) 
3) IN OUT parameter: These types of parameters 
are used to send values and get values from 
stored procedures. 
CREATE [OR REPLACE] PROCEDURE proc3 (param_name IN OUT datatype)
Exception Handling 
• Using Exception Handling we can test the 
code and avoid it from exiting abruptly. 
• When an exception occurs a messages which 
explains its cause is recieved. 
• PL/SQL Exception message consists of three 
parts. 
1) Type of Exception 
2) An Error Code 
3) A message
Structure of Exception Handling 
DECLARE 
Declaration section 
BEGIN 
Exception section 
EXCEPTION 
WHEN ex_name1 THEN 
-Error handling statements 
WHEN ex_name2 THEN 
-Error handling statements 
WHEN Others THEN 
-Error handling statements 
END;
Types of Exception 
a) Named System Exceptions 
b) Unnamed System Exceptions 
c) User-defined Exceptions 
Named System Exceptions
Trigger 
• 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 
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;
Embedded SQL & Dynamic SQL
Embedded SQL Example 
#include<stdio.h> 
#include<string.h> 
EXEC SQL BEGIN DECLARE SECTION; 
long station_id; 
long mon; 
float temp; 
float rain; 
char city_name[21]; 
long SQLCODE; 
EXEC SQL END DECLARE SECTION; 
main() 
{ 
/* the CONNECT statement, if needed, goes here */ 
strcpy(city_name,"Denver"); 
EXEC SQL SELECT ID INTO :station_id 
FROM STATION 
WHERE CITY = :city_name;
if (SQLCODE == 100) 
{ 
printf("There is no station for city %sn",city_name); 
exit(0); 
} 
printf("For the city %s, Station ID is %ldn",city_name,station_id); 
printf("And here is the weather data:n"); 
EXEC SQL DECLARE XYZ CURSOR FOR 
SELECT MONTH, TEMP_F, RAIN_I FROM STATS 
WHERE ID = :station_id 
ORDER BY MONTH; 
EXEC SQL OPEN XYZ; 
while (SQLCODE != 100) { 
EXEC SQL FETCH XYZ INTO :mon, :temp, :rain; 
if (SQLCODE == 100) 
printf("end of listn"); 
else 
printf("month = %ld, temperature = %f, rainfall = %fn",mon,temp,rain); 
} 
EXEC SQL CLOSE XYZ; 
exit(0); 
}
• Execution log: 
For the city Denver, Station ID is 44 
And here is the weather data: 
month = 1, temperature = 27.299999, rainfall = 0.180000 
month = 7, temperature = 74.800003, rainfall = 2.110000 
end of list

More Related Content

PPTX
PLSQL Tutorial
PDF
PLSQL tutorial...
PPTX
ORACLE PL SQL FOR BEGINNERS
PPT
PPTX
PL/SQL Fundamentals I
TXT
PPT
PLSQL Cursors
PPT
PL/SQL Introduction and Concepts
PLSQL Tutorial
PLSQL tutorial...
ORACLE PL SQL FOR BEGINNERS
PL/SQL Fundamentals I
PLSQL Cursors
PL/SQL Introduction and Concepts

What's hot (20)

PPTX
Procedure and Functions in pl/sql
PPTX
PL/SQL - CURSORS
PPTX
Oracle: Procedures
PPTX
PPTX
4. plsql
PDF
Cursors
PPTX
Oracle: Control Structures
PDF
Programming in Oracle with PL/SQL
PPTX
Packages in PL/SQL
PPT
Pl sql guide
PPTX
PL-SQL DIFFERENT PROGRAMS
PPT
DOC
3963066 pl-sql-notes-only
PPTX
pl/sql Procedure
PPTX
Oracle: PLSQL Introduction
PDF
PLSQL CURSOR
PPT
10g plsql slide
PDF
Packages - PL/SQL
PPTX
Introduction to PL/SQL
PPT
Procedure and Functions in pl/sql
PL/SQL - CURSORS
Oracle: Procedures
4. plsql
Cursors
Oracle: Control Structures
Programming in Oracle with PL/SQL
Packages in PL/SQL
Pl sql guide
PL-SQL DIFFERENT PROGRAMS
3963066 pl-sql-notes-only
pl/sql Procedure
Oracle: PLSQL Introduction
PLSQL CURSOR
10g plsql slide
Packages - PL/SQL
Introduction to PL/SQL
Ad

Similar to SQL / PL (20)

PDF
Dynamic websites lec3
PPTX
PLSQL.pptx
PPTX
Pl sql Prograaming of Database management system
PPTX
PL_SQL_1.pptx fvbxcfbhxdfgh .
PPTX
BBarters_PL_SQL gives cdemo details.pptx
PPTX
plsql tutorialhub....
PPT
plsql.ppt
PDF
Oracle PL/SQL online training | PL/SQL online Training
PPT
4 cursors
PPTX
PL_SQL - II.pptx
PDF
PROCEDURAL LANGUAGE/ STRUCTURED QUERY LANGUAGE.pdf
PPTX
Oracle: Cursors
PPTX
Oracle:Cursors
PPTX
Rdbms chapter 1 function
PPTX
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
PDF
Lecture Notes Unit5 chapter 15 PL/SQL Programming
PPT
My cool new Slideshow!
PPTX
Cursors, triggers, procedures
PPTX
3130703_DBMS_GTU_Study_Material_Presentations_Unit-10_17102019083650AM.pptx
Dynamic websites lec3
PLSQL.pptx
Pl sql Prograaming of Database management system
PL_SQL_1.pptx fvbxcfbhxdfgh .
BBarters_PL_SQL gives cdemo details.pptx
plsql tutorialhub....
plsql.ppt
Oracle PL/SQL online training | PL/SQL online Training
4 cursors
PL_SQL - II.pptx
PROCEDURAL LANGUAGE/ STRUCTURED QUERY LANGUAGE.pdf
Oracle: Cursors
Oracle:Cursors
Rdbms chapter 1 function
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
Lecture Notes Unit5 chapter 15 PL/SQL Programming
My cool new Slideshow!
Cursors, triggers, procedures
3130703_DBMS_GTU_Study_Material_Presentations_Unit-10_17102019083650AM.pptx
Ad

Recently uploaded (20)

PDF
Empowerment Technology for Senior High School Guide
PDF
RMMM.pdf make it easy to upload and study
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PDF
Indian roads congress 037 - 2012 Flexible pavement
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PDF
Weekly quiz Compilation Jan -July 25.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
IGGE1 Understanding the Self1234567891011
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PPTX
History, Philosophy and sociology of education (1).pptx
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PDF
1_English_Language_Set_2.pdf probationary
PDF
Trump Administration's workforce development strategy
PDF
Computing-Curriculum for Schools in Ghana
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Empowerment Technology for Senior High School Guide
RMMM.pdf make it easy to upload and study
Paper A Mock Exam 9_ Attempt review.pdf.
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
Indian roads congress 037 - 2012 Flexible pavement
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
Weekly quiz Compilation Jan -July 25.pdf
Cell Types and Its function , kingdom of life
IGGE1 Understanding the Self1234567891011
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
History, Philosophy and sociology of education (1).pptx
Practical Manual AGRO-233 Principles and Practices of Natural Farming
1_English_Language_Set_2.pdf probationary
Trump Administration's workforce development strategy
Computing-Curriculum for Schools in Ghana
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3

SQL / PL

  • 1. PL / SQL By, C.Janani, AP / IT
  • 2. Definition • PL/SQL stands for Procedural Language extension of SQL. • Procedural features of programming languages. • Developed by Oracle Corporation in the early 90’s to enhance the capabilities of SQL. • PL/SQL Block consists of three sections:  The Declaration section (optional).  The Execution section (mandatory).  The Exception (or Error) Handling section (optional). DECLARE Variable declaration BEGIN Program Execution EXCEPTION Exception handling END;
  • 3. Advantages of PL/SQL • Block Structures • Procedural language Capability • Better Performance • Error Handling
  • 4. Conditional Statements in PL/SQL IF THEN ELSE STATEMENT 1) IF condition THEN statement 1; ELSE statement 2; END IF; 2) IF condition 1 THEN statement 1; statement 2; ELSIF condtion2 THEN statement 3; ELSE statement 4; END IF 3) IF condition 1 THEN statement 1; statement 2; ELSIF condtion2 THEN statement 3; ELSE statement 4; END IF; 4) IF condition1 THEN ELSE IF condition2 THEN statement1; END IF; ELSIF condition3 THEN statement2; END IF;
  • 5. Iterative Statements in PL/SQL • Simple Loop • While Loop • For Loop LOOP statements; EXIT; {or EXIT WHEN condition;} END LOOP; WHILE <condition> LOOP statements; END LOOP; FOR counter IN val1..val2 LOOP statements; END LOOP;
  • 6. Cursors • A temporary work area created in the system memory when a SQL statement is executed. • Contains information on a select statement and the rows of data accessed by it. • Can open a cursor, and repeatedly fetch a tuple then move the cursor, until all tuples have been retrieved. – Can use the ORDER BY clause, in queries that are accessed through a cursor, to control the order in which tuples are returned. • Fields in ORDER BY clause must also appear in SELECT clause. • Can also modify/delete tuple pointed to by a cursor.
  • 10. Cursor Types: • 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.
  • 11. Stored Procedures • 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. • Has a header and a body. • 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-parameters • A procedure may or may not return any value.
  • 12. Syntax: • CREATE [OR REPLACE] PROCEDURE proc_name [list of parameters] IS Declaration section BEGIN Execution section EXCEPTION Exception section END;
  • 13. PL/SQL Functions • The major difference between a procedure and a function is, a function must always return a value, but a procedure may or may not return a value. • General Syntax to create a function is CREATE [OR REPLACE] FUNCTION function_name [parameters] RETURN return_datatype; IS Declaration_section BEGIN Execution_section Return return_variable; EXCEPTION exception section Return return_variable; END;
  • 14. Parameters in Procedure and Functions 1) IN type parameter: These types of parameters are used to send values to stored procedures. CREATE [OR REPLACE] PROCEDURE procedure_name ( param_name1 IN datatype, param_name12 IN datatype ... ) 2) OUT type parameter: These types of parameters are used to get values from stored procedures. This is similar to a return type in functions. CREATE [OR REPLACE] PROCEDURE proc2 (param_name OUT datatype) 3) IN OUT parameter: These types of parameters are used to send values and get values from stored procedures. CREATE [OR REPLACE] PROCEDURE proc3 (param_name IN OUT datatype)
  • 15. Exception Handling • Using Exception Handling we can test the code and avoid it from exiting abruptly. • When an exception occurs a messages which explains its cause is recieved. • PL/SQL Exception message consists of three parts. 1) Type of Exception 2) An Error Code 3) A message
  • 16. Structure of Exception Handling DECLARE Declaration section BEGIN Exception section EXCEPTION WHEN ex_name1 THEN -Error handling statements WHEN ex_name2 THEN -Error handling statements WHEN Others THEN -Error handling statements END;
  • 17. Types of Exception a) Named System Exceptions b) Unnamed System Exceptions c) User-defined Exceptions Named System Exceptions
  • 18. Trigger • 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.
  • 19. Syntax of Triggers 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;
  • 20. Embedded SQL & Dynamic SQL
  • 21. Embedded SQL Example #include<stdio.h> #include<string.h> EXEC SQL BEGIN DECLARE SECTION; long station_id; long mon; float temp; float rain; char city_name[21]; long SQLCODE; EXEC SQL END DECLARE SECTION; main() { /* the CONNECT statement, if needed, goes here */ strcpy(city_name,"Denver"); EXEC SQL SELECT ID INTO :station_id FROM STATION WHERE CITY = :city_name;
  • 22. if (SQLCODE == 100) { printf("There is no station for city %sn",city_name); exit(0); } printf("For the city %s, Station ID is %ldn",city_name,station_id); printf("And here is the weather data:n"); EXEC SQL DECLARE XYZ CURSOR FOR SELECT MONTH, TEMP_F, RAIN_I FROM STATS WHERE ID = :station_id ORDER BY MONTH; EXEC SQL OPEN XYZ; while (SQLCODE != 100) { EXEC SQL FETCH XYZ INTO :mon, :temp, :rain; if (SQLCODE == 100) printf("end of listn"); else printf("month = %ld, temperature = %f, rainfall = %fn",mon,temp,rain); } EXEC SQL CLOSE XYZ; exit(0); }
  • 23. • Execution log: For the city Denver, Station ID is 44 And here is the weather data: month = 1, temperature = 27.299999, rainfall = 0.180000 month = 7, temperature = 74.800003, rainfall = 2.110000 end of list