What is PL/SQL
PL/SQL is a procedural language where we write programming logic to the execution of sql commands. pl/sql is bunch of code or sql command that mainly perform some database operation in specified manner or handle database or exception occurred during program execution. we mainly write the rule how sql command execute.
PL/SQL block mainly divided into three section
- 1. declare
- 2. begin
- 3. exception
PL/SQL declaration section : declaration section is a section where we define variable, constant value or cursors that used in begin or executable section to perform task. declaration section start with keyword declare.
PL/SQL Executable section : Executable section of PL/SQL block start with keyword begin. we write executable code(actual logic ) in this section.
PL/SQL Exception handling section : Exception handling section is mainly used to catch and handle user defined or system related exception that occured in executable section of PL/SQL section. Exception handling section is optional section of PL/SQL block.
How to write PL/SQL block
Example of calculating area of circle and insert calculated area of circle in table.
For calculating area of circle we first declare constant pi=3.1415926 and taking two variable radius and area in declaration section and in logic section or begin section we assigned 3 to radius variable and calculating area of circle pi*r2 and assign calculated area to area variable and after calculation we inserted area value in table area.