0% found this document useful (0 votes)
3 views

DBMS Lab- 7 PLSQL - 1

PLSQL is a procedural language that reduces network traffic and allows for error handling, user-defined functions, and database triggers. A PLSQL block consists of three parts: declaration, executable statements, and exception handling, with valid statements including DML and SQL functions. The document also covers syntax for conditional branching, looping, and provides examples along with exercises and viva questions related to PLSQL.

Uploaded by

sunbun2070
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DBMS Lab- 7 PLSQL - 1

PLSQL is a procedural language that reduces network traffic and allows for error handling, user-defined functions, and database triggers. A PLSQL block consists of three parts: declaration, executable statements, and exception handling, with valid statements including DML and SQL functions. The document also covers syntax for conditional branching, looping, and provides examples along with exercises and viva questions related to PLSQL.

Uploaded by

sunbun2070
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

PLSQL - 1

PLSQL: Procedural Language


Advantages:
• Decreases the network traffic i.e. can send a group of statements as a block to the database.
• Control statements are provided
• Runtime errors can be handled
• User defined functions, procedures and packages
• Database triggers

PLSQL block has 3 parts:


Declaration (optional) – for variable declaration
Executable part (mandatory) – for statements
Exception part (optional) – for runtime error handling

Valid statements in PLSQL:


• Plsql select statement
• All DML statements
• TCL statements
• SQL functions, operators
• User defined functions, procedures and packages
• Expressions

Directly not valid in PLSQL block:


DDL statements
DCL statements
Syntax:
[Declare
<Declarations>]
Begin
<Statements>
[Exception
<Error handling>]
END;

To see output on screen: set serveroutput on


The function used to display on screen is: dbms_output.put_line (message);
Examples

1. Program to print welcome:


Begin
Dbms_output.put_line('welcome');
End;
/

2. Program to print welcome to inner block and welcome to outer block:


Begin
Dbms_output.put_line ('welcome to outer block');
Begin
Dbms_output.put_line ('welcome to inner block');
End;
End;
/
Examples:
3. Program to calculate sum of 2 numbers:
Declare
no1 number (3):=&number1;
no2 number (3):=&number2;
sum number(5);
Begin
sum:=no1+no2;
Dbms_output.put_line('Total = '||sum);
End;
/
Conditional Branching:

Syntax (Simple if): IF <condition(s)>


THEN
<Statements>;
END IF;

Syntax (if-else): IF <condition(s)>


THEN
<Statements>;
ELSE
<Statements>;
END IF;

Nested if-else: IF …ELSIF…ELSE…END IF;

Case: CASE <expression>


WHEN condition_1 THEN result_1;
WHEN condition_2 THEN result_2;
...
WHEN condition_n THEN result_n;
ELSE result;
END;
Example:
Program to print grade (using IF)
Declare
V_sub1 number(3);
V_sub2 number(3);
V_total number(5);
V_avg number(3);
V_result varchar2(20);
Begin
V_sub1:= &sub1_marks;
V_sub2:= &sub2_marks;
V_total:=V_sub1+V_sub2;
V_avg:=V_total/2;
If V_avg>=75
then V_result:='Distinction';
elsif (V_avg>=60 AND V_avg<75)
then V_result:='First Class';
elsif ( V_avg>=40 AND V_avg<60)
then V_result:='Second Class';
else
V_result:='Fail';
End if;
Dbms_output.put_line ('Result = '||V_result);
End;
/
Example:
Program to illustrate Case statement
Declare
V_num number:=&enternum;
Begin
Case V_num
When 1 Then Dbms_output.put_line ('Violet');
When 2 Then Dbms_output.put_line ('Indigo');
When 3 Then Dbms_output.put_line ('Blue');
When 4 Then Dbms_output.put_line ('Green');
When 5 Then Dbms_output.put_line ('Yellow');
When 6 Then Dbms_output.put_line ('Orange');
When 7 Then Dbms_output.put_line ('Red');
Else
Dbms_output.put_line ('Invalid input');
End case;
End;
/
LOOPING:

Syntax:

WHILE <condition(s)>
LOOP
<Statement(s)>;
END LOOP;

FOR<variable> IN [reverse] <initial_value>..<final_value>


LOOP
<Statement(s)>;
END LOOP;
Examples:
Program to illustrate while loop
Declare
V_no number (3):=1;
V_sum number(5):=0;
Begin
While V_no<=10
Loop
Dbms_output.put_line (V_no);
V_sum:= V_sum+V_no;
V_no:=V_no+1;
End Loop;
Dbms_output.put_line ('Sum = '||V_sum);
End;
/

Note: EXIT statement can be used to terminate the loop based on a condition
Program to illustrate EXIT statement in while loop
Declare
V_no number (3):=1;
Begin
While V_no<=10
Loop
Dbms_output.put_line (V_no);
V_no:=V_no+1;
If V_no=5
Then Exit;
END IF;
End Loop;
Dbms_output.put_line ('Out of loop');
End;
/
Program to illustrate for loop
Begin
For V_no in 1..10
Loop
If mod(V_no,2)=0
Then Dbms_output.put_line (V_no);
END IF;
End Loop;
End;
/
EXERCISE

1. Write a PLSQL program to compute the Factorial of a given number and display it
2. Write a PLSQL program to display the first ‘n’ digits of the Fibonacci series
3. Write a PLSQL program to read a number and display if it is prime or not
4. Write a PLSQL program to read a number and display if it is an armstrong number or not
experimental Viva Questions:

1) Write the syntax for creating a sequence


2) What happens when an object linked to a synonym is deleted?
3) Give the syntax for for loop and while loop in PLSQL
4) What is the different between ELSIF ladder and the CASE
statement in PLSQL
5) What is the difference between SQL and PL/SQL?
6) Name few schema objects that can be created using PL/SQL?
7) Write the syntax for the CASE statement in PLSQL
8) What are the three basic sections of a PL/SQL block?
9) What are the data types available in PL/SQL?
10) Differentiate between syntax and runtime errors?
References
1. https://www.oracle.com/technetwork/datab
ase/features/plsql/index.html
2. http://www.plsqltutorial.com/
3. https://www.youtube.com/watch?v=NeWFH
AalxR8
4. https://community.oracle.com/community/gr
oundbreakers/database/developer-tools/sql_
and_pl_sql

You might also like