DBMS LAB EXPERIMENT WEEK7
DBMS LAB EXPERIMENT WEEK7
Introduction
The PL/SQL programming language was developed by Oracle Corporation in the late
1980s as procedural extension language for SQL.
PL/SQL is not a stand-alone programming language; it is a tool within the Oracle
programming environment.
PL/SQL is a block-structured language that is PL/SQL programs are divided into
logical blocks of code.
Declarations: This section starts with the keyword DECLARE. It is an optional section and
defines all variables, cursors, subprograms, and other elements to be used in the program.
Executable block: This section is enclosed between the keywords BEGIN and END and it is a
mandatory section. It consists of the executable PL/SQL statements of the program. It
should have at least one executable line of code
Exception Handling: This section starts with the keyword EXCEPTION. This section is again
optional and contains exception(s) that handle errors in the program.
DATA TYPES
1. Number
2. char
3. varchar2
4. date
5. %type it is to continue the variable data type same as in table
VARIABLE DECLARATION
Syntax for declaring a variable is:
variable_name datatype:=initial_value;
Initializing Variables
Regd. No:
Database Management Systems Lab
2 Aditya College of Engineering & Technology
a integer := 10;
Example:
msg char(20) := ‘hello world’;
Control structures
Conditional statements
Repetitive statements
Case statements
Conditional statements
1. Simple IF
Syntax:
IF condition THEN
Statements;
END IF;
2. IF-ELSE
Syntax:
IF condition THEN
Statements;
ELSE
Statements;
END IF;
3. IF-ELSE LADDER
Syntax:
IF condition THEN
Statements;
ELSIF condition THEN
Statements;
…
ELSE
Statements;
END IF;
4. CASE STATEMENTS
Syntax:
Case selector
WHEN 'value1' THEN Statements;
WHEN 'value2' THEN Statements;
WHEN 'value3' THEN Statements;
...
ELSE Statements; default case
END CASE;
LOOPS
1. BASIC LOOP
Regd. No:
Database Management Systems Lab
3 Aditya College of Engineering & Technology
2. WHILE LOOP
3. FOR LOOP
1. BASIC LOOP
Syntax:
LOOP
Statements;
END LOOP;
2. WHILE LOOP
Syntax:
WHILE condition LOOP
Statements;
END LOOP;
3. FOR LOOP
Syntax:
FOR variable_name IN/IN REVERSE start_value .. end_value
LOOP
Statements;
END LOOP; //for j in reverse 10..1
Loop
end loop
Regd. No:
Database Management Systems Lab
4 Aditya College of Engineering & Technology
EXCEPTIONS
Error condition during a program execution is called an exception in PL/SQL.
There are two types of exceptions:
System-defined exceptions
User-defined exceptions
Syntax:
DECLARE
<Declarations section>
BEGIN
<Executable Statements>
EXCEPTION
WHEN exception1 THEN
exception1-handling-statements;
WHEN exception2 THEN
exception2-handling-statements;
WHEN exception3 THEN
exception3-handling-statements;
........
END;
1. Write a PL/SQL Program to display "Hello World" message
Regd. No:
Database Management Systems Lab
5 Aditya College of Engineering & Technology
Regd. No:
Database Management Systems Lab
6 Aditya College of Engineering & Technology
Declare
n int:=&n;
s int:=0;
i int;
begin
for i in 1..10
loop
s := s+i;
end loop;
dbms_output.put_line('Sum of 1 to ' || n || ' number is '|| s);
end;
Output
5. Write PL/SQL program to find multiplication table for a given 'n' value
Aim:- To find multiplication table for a given 'n' value
Source Code:-
Set serveroutput on;
Declare
n int:=&n;
res int:=0;
i int:=1;
begin
loop
res:=(n*i);
dbms_output.put_line(n||'*'||i||'='||res);
Exit when i=10;
i:=i+1;
end loop;
end;
Regd. No:
Database Management Systems Lab
7 Aditya College of Engineering & Technology
Declare
n int:= &n;
s int:=0;
i int;
begin
for i in 1..10
loop
dbms_output.put_line(n || ‘ X ‘ || i || ‘ = ‘ || n*i );
end loop;
end;
Output:-
Regd. No:
Database Management Systems Lab