Introduction To PLSQL

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

PL/SQL

Introduction & Architecture


What is PL/SQL?
• PL/SQL is an extension of Structured Query Language (SQL) that is
used in Oracle.
• PL/SQL allows the programmer to write code in a procedural format.
Full form of PL/SQL is "Procedural Language extensions to SQL".
Advantage of Using PL/SQL
• Better performance, as SQL is executed in bulk rather than a single
statement
• High Productivity
• Tight integration with SQL
• Full Portability
• Tight Security
• Support Object Oriented Programming concepts.
Difference between SQL and PL/SQL
SQL PL/SQL
•SQL is a single query that is used to •PL/SQL is a block of codes that used to
perform DML and DDL operations. write the entire program blocks/
procedure/ function, etc.
•It is declarative, that defines what need to •PL/SQL is procedural that defines how the
be done, rather than how things need to things needs to be done.
be done.
•Execute as a single statement. •Execute as a whole block.

•Mainly used to manipulate data. •Mainly used to create an application


What is PL/SQL block?
• In PL/SQL, the code is not executed in single line format, but it is
always executed by grouping the code into a single element called
Blocks.
• Blocks contain both PL/SQL as well as SQL instruction. All these
instruction will be executed as a whole rather than executing a single
instruction at a time.
Block Structure
• PL/SQL blocks have a pre-defined structure in which the code is to be grouped. Below are different sections
of PL/SQL blocks.
• Declaration section
• Execution section
• Exception-Handling section
Example..

set SERVEROUTPUT ON

BEGIN
dbms_output.put_line ('Hello World..');
END;
Declaring and usage of variables in the
program
declare
msg varchar(25);
BEGIN
msg := 'Hello NIBM';
dbms_output.put_line (msg);
END;
/
Comments in PL/SQL
• Commenting code simply instructs the compiler to ignore that
particular code from executing.
• Comment can be used in the program to increase the readability of
the program. In PL/SQL codes can be commented in two ways.
• Using '--' in the beginning of the line to comment that particular line.
• Using '/*…….*/' we can use multiple lines. The symbol '/*' marks the
starting of the comment and the symbol '*/' marks the end of the
comment. The code between these two symbols will be treated as
comments by the compiler.
Data types
• CHARACTER Data Type:
• This data type basically stores alphanumeric characters in string
format.
• Varchar
• char
Data types
• NUMBER Data Type:
• A NUMBER(8,2);
• B NUMBER(8);
Data types
• DATE Data Type:
• newyear DATE:='01-JAN-2015';
• current_date DATE:=SYSDATE;
IF-THEN Statement
DECLARE
a NUMBER :=10;
BEGIN
dbms_output.put_line(‘Program started.' );
IF( a > 100 ) THEN
dbms_output.put_line('a is greater than 100');
END IF;
dbms_output.put_line(‘Program completed.');
END;
/
IF-THEN-ELSE Statement
DECLARE
a NUMBER:=11;
BEGIN
dbms_output.put_line ('Program started');
IF( mod(a,2)=0) THEN
dbms_output.put_line('a is even number' );
ELSE
dbms_output.put_line('a is odd number1');
END IF;
dbms_output.put_line ('Program completed.');
END;
/
IF-THEN-ELSIF Statement
DECLARE
mark NUMBER :=55;
BEGIN
dbms_output.put_line('Program started.' );
IF( mark >= 70) THEN
dbms_output.put_line('Grade A');
ELSIF(mark >= 40 AND mark < 70) THEN
dbms_output.put_line('Grade B');
ELSIF(mark >=35 AND mark < 40) THEN
dbms_output.put_line('Grade C');
END IF;
dbms_output.put_line('Program completed.');
END;
/
Find max number among 3 numbers
DECLARE
a NUMBER :=10;
b NUMBER :=15;
c NUMBER :=20;
BEGIN
dbms_output.put_line('Program started.' );
IF( a > b)THEN
/*Nested-if l */
dbms_output.put_line('Checking Nested-IF 1');
IF( a > c ) THEN
dbms_output.put_line('A is greatest');
ELSE
dbms_output.put_line('C is greatest');
END IF;
ELSE
/*Nested-if2 */
dbms_output.put_line('Checking Nested-IF 2' );
IF( b > c ) THEN
dbms_output.put_line('B is greatest' );
ELSE
dbms_output.put_line('C is greatest' );
END IF;
END IF;
dbms_output.put_line('Program completed' );
END;
/
For Loop
BEGIN
dbms_output.put_line('Program started.');
FOR a IN 1 .. 5
LOOP
dbms_output.put_line(a);
END LOOP;
dbms_output.put_line('Program completed.');
END;
/
While Loop
DECLARE
a NUMBER :=1;
BEGIN
dbms_output.put_line('Program started');
WHILE (a < 5)
LOOP
dbms_output.put_line(a);
a:=a+1;
END LOOP;
dbms_output.put_line('Program completed' );
END;
/

You might also like