Dbms Unit 4 PLSQL

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

PL/SQL

https://apex.oracle.com/pls/apex/f?p=4500:1003:100115258005547:::::

https://apex.oracle.com/
PL/SQL
1. Procedural Language /Standard Query Language.
2. Significant member of oracle programing Tool set
3. Extensively used to code server side programming
4. Case-insensitive programming language
Generally a program written in PL/SQL language is divided in to blocks.
Blocks
• Blocks are basic programming unit in PL/SQL Programming language.
• There are two types of blocks in PL/SQL:
1. Anonymous Block
2. Named block
Both type of PL/SQL blocks are further divided in to three dufferent
section which are
1.The declaration section
2. The execution section
3. The Exception-handling section
• The execution section is the only mandatory section of blocks whereas declaration and
exception handling section are optional.
• The basic prototype of anonymous PL/SQL block.
• DECLARE

• <declarations section>

• BEGIN

• <executable command(s)>

• EXCEPTION

• <exception handling goes here >

• END;
Declaration Section
• First section of PL/SQL block
• Contains definition of PL/SQL identifiers such as variable, constants,
cursors
• All local variable used in the program are defined and documented
• Start with keyword DECLARE.
Execution Section
• Contains executable statements that allow you to manipulate the
variables that have been declared in the declaration section
• The execution section of any PL/SQL block always begins with the
keyword BEGIN and Ends with the keyword END.
• Only mandatory section in PL/SQL block
• This section support all DML commands and SQL+ build in function,
and it also supports DDL commands using native Dynamic SQL or
DBMS_SQL build-in package.
The Exception-handling section
• This is the last section of PL/SQL block which is optional just like the
declaration section.
• This section contain statements that are executed when a runtime
error occurs within the block
• We can sat allException-handling code goes here
Hello World Program in PL/SQL
begin
dbms_output.put_line('Hello World');
end;
• dbms_output.put_line(): It is used for printing the output to the console screen. It is
due to output activation that we write ‘set serveroutput on’ at start of the program.
dbms_output.put_line() 
DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
PL/SQL Program To Add Two
Numbers
DECLARE
a integer := 30;
b integer := 40;
c integer;
BEGIN
c := a + b;
dbms_output.put_line('Value of c: ' || c);
END;
DECLARE
f real;
BEGIN
f := 100.0/3.0;
dbms_output.put_line('Value of f:'||f);
END;
Variable Scope in PL/SQL:
PL/SQL allows nesting of blocks. A program block can contain another inner block.
If you declare a variable within an inner block, it is not accessible to an outer block.
There are two types of variable scope:
Local Variable: Local variables are the inner block variables which are not accessible
to outer blocks.
Global Variable: Global variables are declared in outermost block.
Example of Local and Global variables
DECLARE
-- Global variables
num1 number := 95;
num2 number := 85;
BEGIN
dbms_output.put_line('Outer Variable num1: ' || num1);
dbms_output.put_line('Outer Variable num2: ' || num2);
DECLARE
-- Local variables
num1 number := 195;
num2 number := 185;
BEGIN
dbms_output.put_line('Inner Variable num1: ' || num1);
dbms_output.put_line('Inner Variable num2: ' || num2);
END;
END;
/
Example of PL/SQL constant
DECLARE
-- constant declaration
pi constant number := 3.141592654;
-- other declarations
radius number(5,2);
dia number(5,2);
circumference number(7, 2);
area number (10, 2);
BEGIN
-- processing
radius := 9.5;
dia := radius * 2;
circumference := 2.0 * pi * radius;
area := pi * radius * radius;
-- output
dbms_output.put_line('Radius: ' || radius);
dbms_output.put_line('Diameter: ' || dia);
dbms_output.put_line('Circumference: ' || circumference);
dbms_output.put_line('Area: ' || area);
END;
Example of PL/SQL If Statement

DECLARE
a number(3) := 500;
BEGIN
-- check the boolean condition using if statement
IF( a < 20 ) THEN
-- if condition is true then print the following
dbms_output.put_line('a is less than 20 ' );
ELSE
dbms_output.put_line('a is not less than 20 ' );
END IF;
dbms_output.put_line('value of a is : ' || a);
END;
PL/SQL Case Statement
• The PL/SQL CASE statement facilitates you to execute a
sequence of statements based on a selector. A selector can be
anything such as variable, function or an expression that the
CASE statement checks to a boolean value.
• The CASE statement works like the IF statement, only using the
keyword WHEN. A CASE statement is evaluated from top to
bottom. If it get the condition TRUE, then the corresponding
THEN calause is executed and the execution goes to the END
CASE clause.
DECLARE
grade char(1) := 'A';
BEGIN
CASE grade
when 'A' then dbms_output.put_line('Excellent');
when 'B' then dbms_output.put_line('Very good');
when 'C' then dbms_output.put_line('Good');
when 'D' then dbms_output.put_line('Average');
when 'F' then dbms_output.put_line('Passed with Grace');
else dbms_output.put_line('Failed');
END CASE;
END;
PL/SQL Loop
The PL/SQL loops are used to repeat the execution of one or more statements for
specified number of times. These are also known as iterative control statements.
Syntax for a basic loop:
LOOP
Sequence of statements;
END LOOP;
Types of PL/SQL Loops
There are 4 types of PL/SQL Loops.
Basic Loop / Exit Loop
While Loop
For Loop
Cursor For Loop
PL/SQL Exit Loop (Basic Loop)

PL/SQL exit loop is used when a set of statements is to be executed at least once before the
termination of the loop. There must be an EXIT condition specified in the loop, otherwise the loop will
get into an infinite number of iterations. After the occurrence of EXIT condition, the process exits the
loop.
Example:

DECLARE
i NUMBER := 1;
BEGIN
LOOP
EXIT WHEN i>=10;
DBMS_OUTPUT.PUT_LINE(i);
i := i+1;
END LOOP;
END;
PL/SQL While Loop
PL/SQL while loop is used when a set of statements has to be executed as long as a
condition is true, the While loop is used. The condition is decided at the beginning of
each iteration and continues until the condition becomes false.
Example:

DECLARE
i INTEGER := 1;
BEGIN
WHILE i <= 10 LOOP
DBMS_OUTPUT.PUT_LINE(i);
i := i+1;
END LOOP;
END;
PL/SQL FOR Loop
PL/SQL for loop is used when when you want to execute a set of statements
for a predetermined number of times. The loop is iterated between the start
and end integer values. The counter is always incremented by 1 and once the
counter reaches the value of end integer, the loop ends.
Example:
Example2 :
BEGIN
FOR k IN 1..10 LOOP DECLARE
VAR1 NUMBER;
-- note that k was not declared BEGIN
DBMS_OUTPUT.PUT_LINE(k); VAR1:=10;
FOR VAR2 IN 1..10
END LOOP; LOOP
END; DBMS_OUTPUT.PUT_LINE (VAR1*VAR2);
END LOOP;
END;
PL/SQL GOTO Statement

In PL/SQL, GOTO statement makes you able to get an unconditional jump from the GOTO
to a specific executable statement label in the same subprogram of the PL/SQL block.
DECLARE
a number(2) := 30;
BEGIN
<<loopstart>>
-- while loop execution
WHILE a < 50 LOOP
dbms_output.put_line ('value of a: ' || a);
a := a + 1;
IF a = 35 THEN
a := a + 1;
GOTO loopstart;
END IF;
END LOOP;
END;
Create table in PL/SQL
CREATE TABLE customers
( customer_id number(10) NOT NULL,
customer_name varchar2(50) NOT NULL,
city varchar2(50)
);
Insert in to the table:
BEGIN
INSERT INTO customers VALUES (1, 'FLEX', 'bhopal');
INSERT INTO customers VALUES (2, 'HARRY', 'chennai');
END
Display table:
SELECT * from customers;

drop table customers;


Update
BEGIN
UPDATE customers SET customer_name = 'ALAN' WHERE customer_id =
1;
END;

BEGIN
UPDATE customers SET customer_name = ‘JOHN’, city = ‘hydrabad’
WHERE customer_id = 1;
END;
BEGIN
     DELETE FROM EMPLOYEE WHERE CODE=2;
END;

delete

BEGIN
  DELETE FROM VEHICLE;
END;
Delete
BEGIN
DELETE FROM customers WHERE customer_id=2;
END;

BEGIN
DELETE FROM customers;
END;

You might also like