Introduction to PL/SQL
PL/SQL is nothing but a Procedural language which includes SQL. PL/SQL includes programming concepts such as using variables, IF..THEN ie condition branching, Loops, Error Handling etc. PL/SQL combines the SQL power and processing ability to give the best in the database industry.
Blocks
The structure of a block in the PL/SQL programming language is a program through which you can write a PL/SQL. It starts with DECLARE section where you declare all the variables you need in the block, but its an optional what it means is there is no need to have declare section if you dont have any variables. Next comes BEGIN section which is mandatory followed by EXCEPTION section where you handle the errors and the block ends with a END; Usually a Anonymous Block can be executed only once and usually you save the code in a file.
Block Structure
DECLARE
--where you declare variables like
var_customer number(4); -- declaring a variable named
var_customer of data type number.
var_numofrows number(6)
BEGIN
-- where you actually perform the operation
-- embedded select inside the PL/SQL Block
Select count(*) into var_numofrows from invoices where
customer_no = var_customer;
-- Display the value which you got
dbms_output.put_line('The number of invoices we have for the
customer ' || var_customer || ' is ' || var_numofrows);
EXCEPTION
-- where you handle the error;
END;
PL/SQL Sub programs in Oracle database
Stored Procedures
Stored Functions
Package Procedures and Functions
Database Triggers
Advantages of PL/SQL
Modularity
Reusability
Maintenance
Abstraction
Performance
Data Integrity
Data security