Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit 4 K PLSQL Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 60

Unit 4. PL/SQL Programming Ms.S.A.

Kshirsagar

UNIT- IV PL/SQL PROGRAMMING


(MARKS: 18M)
COURSE OUTCOMES:
CO4 - Implement PL/SQL codes for given application.

THEORY LEARNING OUTCOMES:


TLO 4.1 Use control Structures in PL-SQL.
TLO 4.2 Handle different types of exceptions.
TLO 4.3 Explain various types of cursors.
TLO 4.4 Create Procedure, Function on given problem.
TLO 4.5 Explain types of triggers with examples

CONTENTS:
4.1 Introduction of PL/SQL: -Advantages of PL/SQL, The PL/SQL Block
Structure, PL/SQL Data Types, Variable, Constant
4.2 Control Structure :- Conditional Control, Iterative Control, Sequential Control.
4.3 Exception handling: -Predefined Exception, User defined Exception.
4.4 Cursors:- Implicit and Explicit Cursors, Declaring, opening and closing cursor,
fetching a record from cursor, cursor for loops, parameterized cursors
4.5 Procedures :- Advantages, Create, Execute and Delete a Stored Procedure
4.6 Functions :- Advantages, Create, Execute and Delete a Function
4.7 Database Triggers :- Use of Database Triggers, Types of Triggers, Create
trigger, delete trigger

SYCM-Computer Technology Department, SVCP, Pune Page 1


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

4.1 Introduction of PL/SQL: -Advantages of PL/SQL, The PL/SQL Block


Structure, PL/SQL Data Types, Variable , Constant

Introduction of PL/SQL:

PL/SQL stands for “Procedural language” extension to SQL.”PL/SQL” is the


extension of structured Query language.
Advantages of PL/SQL:

1. PL/SQL is portable and high transaction processing language.


2. PL/SQL is in fact procedural language but it also supports object oriented
programming.
3. It allows user to write as well as access the functions and procedures from
outside the programs.
4. It has got built in libraries of packages.
5. PL/SQL is highly productive as it works with the oracle forms to develop the
application software i.e. it works with the front ends to develop the complete
commercial applications.
6. The performance of PL/SQL is better, as in single line query entire block of
statements can be Processed.
7. Some special features of PL/SQL includes the different data types that are
capable of handling different types of data required in real life applications.
8. The most important features like triggers, cursors, locks etc have made PL/SQL
a very versatile language.

9. Security can be ensured with the help of PL/SQL, while developing a


commercial database system.
10. PL/SQL is a user friendly language and very simple to use.

SYCM-Computer Technology Department, SVCP, Pune Page 2


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

PL/SQL Block structure


Explain PL/SQL Block structure.
Declare

Declarations of memory variables;

Begin

SQL executable statements;

Exception
[Exception handling errors]

END;
/

Each PL/SQL program consists of SQL and PL/SQL statements which form a
PL/SQL block.
A PL/SQL Block consists of following sections:
The Declaration section: Declaration of memory variables, constants, records,
and cursors used later in begin section. DECLARE keyword is used for
declaration. This section is not mandatory i.e. as needed this section is used.

The Execution section: It starts with the reserved keyword BEGIN and ends with
END .This is mandatory section. It contains SQL executable statements, program
logic code, loops, conditional statements etc. all this in BEGIN...END block.

The Exception section: It starts with reserved keyword ‘EXCEPTION’. This


section is optional i.e. not mandatory An errors in the program handled in this
section.

End: Mark the end of PL-SQL block

SYCM-Computer Technology Department, SVCP, Pune Page 3


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

PL/SQL Execution Environment:

DECLARE

---------- PL/SQL engine

BEGIN

----------

EXCEPTION

------------ SQL statement


executer
END

PL/SQL execution environment contain PL/SQL engine and SQL statement


executor
Declaration section and begin section executed by PL/SQL engine.
Executable statements written in begin
Exception part executed by SQL statement executor.

PL/SQL Datatypes:

The default data types that can be declared in Pl/SQL are :

1) number: – for storing numeric data


e.g. FLOAT, INT, INTEGER, NUMBER, DEC, DECIMAL, etc.

2)character and string types – for character data


e.g.CHAR, CHARACTER, LONG, RAW, VARCHAR, VARCHAR2 etc

3)date and time – for date and time data


e.g.date , timestamp, timestamp_with_timezone, interval_year_to_monthetc
SYCM-Computer Technology Department, SVCP, Pune Page 4
Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

4) Boolean – for storing true, false or null


e.g.Boolean;

5) %Type - to declare a variable or constant to have a same data type as that of a


previously defined variable or a column in a table.
e.g. eno emp.empno%type

(syntax used : variablename tablename.columnname%type)

Variables and Constants:


Variable:
Variables is location / place holders that store the values/ data . It can change
through PL/SQL block.

Variable declaration:
Syntax
variable_name datatype(size) ;

e.g. salary number(6);


ename varchar(15);
Variable Initialization: To give value while declaration
Syntax
variable_name datatype(size):=value ;
example:
salary number(6):=65000; ….value is given
ename varchar(15):=’nisha’;
dept varchar(10) NOT NULL:=’HR’;
The 3rd example the value is not null i.e. if the value is not given it will take ‘HR’
default value.

Constant:

A constant is a user defined value that remains unchanged throughout the


program.

Constant declaration:

Variable_name constant datatype(size): =value;

SYCM-Computer Technology Department, SVCP, Pune Page 5


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

e.g.
salary_increment CONSTANT number(3):=1000;

 To take/accept values/input from user use following syntax:

varablename datatype(size):=&variablename;
e.g.
n number(10):=&n;
a number(10):=&a;

 To display output use following statement/syntax in pl/sql:

dbms_output.put_line(‘statement’||variable);

e.g:
dbms_output.put_line(‘Hello world!’); --o/p : Hello world!

Suppose n=5 , so
dbms_output.put_line(n); --o/p: 5
dbms_output.put_line(n||’is positive’); --o/p: 5 is positive
dbms_output.put_line(‘the positive value is’||n); --o/p:the positive value is 5
dbms_output.put_line(‘the value’||n||’is positive’); --o/p:the value 5 is positive

SYCM-Computer Technology Department, SVCP, Pune Page 6


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

4.2 Control Structure:- Conditional Control, Iterative Control, Sequential


Control.

4.2.1. Conditional control

There is one conditional statement:


1)The IF statements: It has following forms,
1. IF THEN
2. IF THEN ELSE
3. IF THEN ELSE IF
4. NESTED IF THEN

1)IF THEN

SYANTAX:

If condition THEN
Statements;
END IF;

Explanation:
If condition true, then the statements are executed/run. Otherwise IF condition is
false then if statement does nothing i.e control gets out of end if..
e.g.
set serveroutput on;

declare
n number(2):=&n;
begin
if(n>20) then
dbms_output.put_line(n||’ is positive');
end if;
end;
/
o/p:
enter value for n:=10
10 is positive

PL/SQL procedure successfully completed.


SYCM-Computer Technology Department, SVCP, Pune Page 7
Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

2)
set serveroutput on;
declare
n number(4):=15; --value of n taken while declaration
begin
if(n<20) then
dbms_output.put_line('n is less than 20');
end if;
end;
/

o/p:
n is less than 20
PL/SQL procedure successfully completed.

2)IF THEN ELSE


SYANTAX:

If condition THEN
Statement1;
ELSE
Statement2;
END IF;

Explanation:
If condition is true ,then the statement1 executed/run and control goes out of end
if. otherwise if condition is false statement2 executed/run and control goes out of
end if.
e.g.
set serveroutput on;
declare
age number(4):=&age;
begin
if(age>18) then
dbms_output.put_line('you can vote');
else
dbms_output.put_line('you can not vote');
end if;
end;
/
SYCM-Computer Technology Department, SVCP, Pune Page 8
Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

o/p
Enter value for age:=23
You can vote
PL/SQL procedure successfully completed.

2) pl/sql program to check the no. is even or odd


set serveroutput on;
declare
n number:=&n;
begin
if (mod(n,2)=0) then
dbms_output.put_line(n||’is even’);
else
dbms_output.put_line(n||’is odd’);
end if;
end;
/
o/p

Enter value for n:23


23 is odd
PL/SQL procedure successfully completed.

3) pl/sql program to check the no. is positive or negative


set serveroutput on;
declare
n number:=&n;
begin
if (n>0) then
dbms_output.put_line(n||’is positie’);
else
dbms_output.put_line(n||’is negative’);
end if;
end;
/
o/p
Enter value for n: -14
-14 is negative
PL/SQL procedure successfully completed.

SYCM-Computer Technology Department, SVCP, Pune Page 9


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

3) IF THEN ELSE IF
SYANTAX:

If condition1THEN
Statement1;
ELSE IF condition2 THEN
Statement2;
ELSE IF condition3 THEN
Statement3;
ELSE
Statement_n;
END IF ;
END IF:
END IF;

Explanation:
If then else if is used when there are multiple(more than one ) conditions
if condition1 is true then statement1 is execute and control goes out of if, if
condition1 is false then it will check next condition2 if it is true it execute
statement2 and control goes out of if, if condition2 is false it will checks next
conditions , and execute respective statement which condition is true, and control
goes out of if.
if all conditions false then it will execute sttementn in else part. and control goes
out of else.
Example:
1) pl/sql program to check the no. is positive , negative or zero.
set serveroutput on;
declare
n number:=&n;
begin
if (n>0) then
dbms_output.put_line(‘n is positie’);
else if(n<0)
dbms_output.put_line(‘n is negative’);
else
dbms_output.put_line(‘n is zero’);
end if;
end if;
end;
SYCM-Computer Technology Department, SVCP, Pune Page 10
Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

/
o/p
Enter value for n: -14
-14 is negative
PL/SQL procedure successfully completed.

2)PL/SQL program that accept percentage and displays grade.


set serveroutput on;
declare
percentage number(3):=&percentage;
begin
if(percentage>=75) then
dbms_output.put_line('distinction');
else if(percentage>=60 and percentage<75) then
dbms_output.put_line('first class');
else if(percentage>=45 and percentage<60) then
dbms_output.put_line('second class');
else if(percentage>=40 and percentage<45) then
dbms_output.put_line('pass class');
else
dbms_output.put_line('fail');
end if;
end if;
end if;
end if;
end;
/

o/p
enter value for percentage:65
first class
PL/SQL procedure successfully completed.

e.g.3.
declare
n number(4):=100;
begin
if(n=10) then
dbms_output.put_line('the value of n is 10');
else if(n=20) then
SYCM-Computer Technology Department, SVCP, Pune Page 11
Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

dbms_output.put_line('the value of n is 20');


else if(n=30) then
dbms_output.put_line('the value of n is 30');
else
dbms_output.put_line('the value of n is not matching in given conditions');
end if;
end if;
end if;
end;
/

o/p:
the value of n is not matching in given conditions
PL/SQL procedure successfully completed.

NESTED IF THEN
Nested if means the if within if
Any number of else if clause can be used with one if.
Each nested if statement must terminate with corresponding end if.

SYNTAX:
if condtion1 then
if condition2 then
statement1;
else
statement2;
end if;
else if condition3 then
statement3;
else
statement_x;
end if;
end if;

Explanation:
If condition1(outer if) is true, it will checks next condition(inner if) condition2, if
condition2 is true ,it execute statement1 and if condition2 is false it execute
statement2.

SYCM-Computer Technology Department, SVCP, Pune Page 12


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

if condition1(outer if) is false , it will checks condition3, if condition3 is true it


executes statement3, likewise,…if all conditions are false it execute statement_x of
else.

Example: pl/sql program to check greatest number among 3 numbers.


set serveroutput on;
declare

a number:=&a;

b number:=&b;

c number:=&c;

begin

if a>b then

if a>c then

dbms_output.put_line('a is greater');

else

dbms_output.put_line('c is greater');

end if;

else if b>c then

dbms_output.put_line('b is greater');

else

dbms_output.put_line('c is greater');

end if;

end if;

end;

SYCM-Computer Technology Department, SVCP, Pune Page 13


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

O/P:
Enter value for a: 12
Enter value for b: 14
Enter value for c: 8
b is greater

PL/SQL procedure successfully completed.

4.2.2. Iterative Control:

LOOP: PL/SQL LOOP statement is an iterative control statement that allows to


execute a sequence of statements repeatedly like WHILE and FOR loop.

PL/SQL provides following types of loop statements:


1)Basic loop
2)While loop
3)For loop and Reverse For loop
4)Nested loops

1)BASIC LOOP:
It encloses sequence of statements in between LOOP and END LOOP.
EXIT or EXIT WHEN statement is required to break the loop.

Syntax:

LOOP
sequence_of_statements;
END LOOP;

Example:
set serveroutput on;
declare
a number:=10;
begin
loop
dbms_output.put_line(a);
a:=a+10;
exit when(a>60);
end loop;
end;
SYCM-Computer Technology Department, SVCP, Pune Page 14
Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

/
10
20
30
40
50
60
PL/SQL procedure successfully completed

2) WHILE: WHILE LOOP statement in PL/SQL repeatedly executes a target


statement as long as a given condition is true.
Syntax:

WHILE condition
LOOP
sequence_of_statements;
END LOOP;

Example:
declare
a number(3):=11;
begin
while a<=20
loop
dbms_output.put_line(a);
a:=a+1;
end loop;
end;
/
o/p:
11
12
13
14
15
16
17
18
19
20
SYCM-Computer Technology Department, SVCP, Pune Page 15
Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

PL/SQL procedure successfully completed.

Above example prints the value from 11 to 20 by using while loop.

2) PL/SQL program that prints table of 5 using while loop.

set serveroutput on;


declare
n number(3):=5;
i number(5):=1;
begin
for i in 1..10
loop
dbms_output.put_line(i*n);
end loop;
end;
/

3) PL/SQL program that calculates factorial of 10 using while loop.


set serveroutput on;
declare
n number(3):=10;
fact number:=1;
begin
while(n>0)
loop
fact:=fact*n;
n:=n-1;
end loop;
dbms_output.put_line(fact);
end;
/

SYCM-Computer Technology Department, SVCP, Pune Page 16


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

3) FOR: FOR loop is an iterative statement that executes a sequence of statements


a fixed number of times.

Syntax:
FOR counter IN initial_value..final_value
LOOP
sequence_of_statements;
END LOOP;

Example: write a PL/SQL program to print numbers from 50 to 60 using for


loop

set serveroutput on;


declare
a number(3);
begin
for a in 50..60
loop
dbms_output.put_line(a);
end loop;
end;
/

50
51
52
53
54
55
56
57
58
59
60

PL/SQL procedure successfully completed.

SYCM-Computer Technology Department, SVCP, Pune Page 17


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

REVERSE FOR LOOP


Syntax:

FOR counter IN REVERSE initial_value..final_value


LOOP
sequence_of_statements;
END LOOP;

Example: to print 50 to 60 in reverse order using for loop


set serveroutput on;
declare
a number(3);
begin
for a in reverse 50..60
loop
dbms_output.put_line(a);
end loop;
end;
/
o/p:
60
59
58
57
56
55
54
53
52
51
50

PL/SQL procedure successfully completed.

SYCM-Computer Technology Department, SVCP, Pune Page 18


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

Nested loops in PL/SQL:

PL/SQL allows using one loop inside another loop(like basic,while,for loop)

Syntax:

loop

sequence_of_statements_1;

loop

sequence_of_statements_2

end loop;

end loop;

example:

set serveroutput on;

declare o/p

i number; 1

j number; 2

3
begin
1
for i in 1..3 loop
2
for j in 1..3 loop
3
dbms_output.put_line(j);
1
end loop; 2
end loop; 3

end;

SYCM-Computer Technology Department, SVCP, Pune Page 19


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

2) pl/sql program to print prime numbers between 1 to 50

DECLARE

i NUMBER(3);

j NUMBER(3);

BEGIN

dbms_output.Put_line('The prime numbers are:');

i := 2;

LOOP

j := 2;

LOOP

EXIT WHEN( ( MOD(i, j) = 0 )

OR ( j = i ) );

j := j + 1;

END LOOP;

IF( j = i )THEN

dbms_output.Put_line(i||' ');

END IF;

i := i + 1;

exit WHEN i = 50;

END LOOP;

END;

SYCM-Computer Technology Department, SVCP, Pune Page 20


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

4.2.3. Sequential Statements


1) CASE
2) CONTINUE
3) GOTO

CASE:
A case statement uses a selector.
The CASE statement selects one sequence of statements/action to execute
depending on selector.
Here selector is variable or expression.
The case statement checks value of selector one by one from top to bottom and
execute the corresponding statement when it finds valid value of selector And
control goes to end case.

SYNTAX:
CASE selector
When value1 then s1;
When value2 then s2;
When value3 then s3;
.
.
.
.
Else Sn ; … default case
End case;

Here, selector is variable. The value of selector will be checked with values
sequentially as soon as the value of selector is matched, the corresponding
statement of matched value will get execute and control goes out of end case, if no
any values are matched then, it execute statement in else i.e sn.

Example:pl/sql program to disply day of week using case statement.

set serveroutput on;


declare
daynum number:=&daynum;
begin
case daynum

SYCM-Computer Technology Department, SVCP, Pune Page 21


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

when 1 then dbms_output.put_line('Monday');


when 2 then dbms_output.put_line('Tuesday');
when 3 then dbms_output.put_line('Wedensday');
when 4 then dbms_output.put_line('Thursday');
when 5 then dbms_output.put_line('Friday');
when 6 then dbms_output.put_line('Saturday');
when 7 then dbms_output.put_line('Sunday');
else
dbms_output.put_line('value not matching');
end case;
end;
/

o/p:
Enter value for daynum: 5
Friday
PL/SQL procedure successfully completed.

e.g. pl/sql program for grade (take value while declaration) using case
statement

declare
grade varchar(15):='A';
begin
case grade
when 'A' then dbms_output.put_line('first class’);
when 'B' then dbms_output.put_line('second class');
when 'C' then dbms_output.put_line('pass');
elsedbms_output.put_line('no value of grade is matching');
end case;
end;
/
o/p: first class
PL/SQL procedure successfully completed.

SYCM-Computer Technology Department, SVCP, Pune Page 22


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

2) CONTINUE
continue statement skip the current iteration and start the next iteration.
Syntax:

continue;

example:
set serveroutput on;
declare
i number ;
begin
for i in 1..5 loop
if i=3 then
continue;
end if;
dbms_output.put_line(i);
end loop;
end;
/
o/p:
1
2
4
5
e.g :2) PL/SQL program to calculate addition of even numbers between 1 to
10 by skipping odd numbers using continue.
set serveroutput on;
declare
sum number:=0;
i number;
begin
for i in 1..10 loop
if mod(i,2)!=0 then
continue;
end if;
sum:=sum+i;
end loop;
dbms_output.put_line(sum);
end;
/
SYCM-Computer Technology Department, SVCP, Pune Page 23
Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

o/p:
30

3)GOTO

goto statement transfers control to a labeled statement within the same block or
subprogram .
Syntax:
goto label_name;

to define label use following syntax:


<<label_name>>

Example:
BEGIN
GOTO second_output;

DBMS_OUTPUT.PUT_LINE('This line will never execute.');

<<second_output>>
DBMS_OUTPUT.PUT_LINE('We are here!');
END;
/

o/p:
We are here!

SYCM-Computer Technology Department, SVCP, Pune Page 24


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

Example. 2) PL/SQL program to display 1 to 10 numbers when no. is 5 then


skip using goto.

set serveroutput on;


declare
i number:=1;
begin
while i<=10 loop
if(i=5) then
goto skip;
end if;
dbms_output.put_line(i);
<<skip>>
i:=i+1;
end loop;
end;
/

o/p
1
2
3
4
6
7
8
9
10

SYCM-Computer Technology Department, SVCP, Pune Page 25


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

4.3 Exception handling: -Predefined Exception, User defined Exception.

(Explain the exception handling with its two type. 4M)

Exception- Exception is nothing but an error


Exception Handling:”
Exception handling is nothing but a code block in memory that will attempt to
resolve current error condition.
Exception can be raise when DBMS encounters errors or it can be raised
explicitly. When the system throws a warning or has an error it can lead to an
exception. Such exception needs to be handled and can be defined internally or
user defined.

Syntax:

DECLARE
Declaration section
BEGIN
…executable statement;
EXCEPTION
WHEN exception_name THEN
Error handling statements/user defined action to be carried out;
END;

Types of Exception:

1)Predefined Exception
2)User defined exception

1) Predefined Exception/system defined exception/named exception:


These exceptions Are always automatically raised whenever related error occurs.
The most common errors that can occur during the execution of PL/SQL.
The some predefined exception/errors are:
no_data_found
zero_divide
Cursor_already_open,
invalid_cursor
too_many_rows etc.
these errors/exception of Programs are handled by system defined Exceptions.

SYCM-Computer Technology Department, SVCP, Pune Page 26


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

Example:
Pl/sql program for Zero_divide exception .

set serveroutput on;


declare
a number:=&a;
b number:=&b;
c number;
begin
dbms_output.put_line('first number is'||a);
dbms_output.put_line('second number is'||b);
c:=a/b; --raise exception if b is 0
dbms_output.put_line('result is'||c); --result not displayed if b is 0
exception
when zero_divide then
dbms_output.put_line('trying to divide by zero error');
end;
/
Output:
Enter value for a:20
Enter value for b:0
first number is 20
second number is 0
trying to divide by zero error
PL/SQL procedure successfully completed.

SYCM-Computer Technology Department, SVCP, Pune Page 27


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

Example.2) using
NO_DATA_FOUND exception

Q)write pl/sql program that retrieves the salary of an employee based on their
emp_id.if the employee id does not exist in the database ,handle the
NO_DATA_FOUND exception and print message saying ”employee id not
found”

Consider, employee table

EMP_ID EMP_NAME SALARY


---------- --------------- ----------
1 a 45000
2 b 35000
3 c 40000
4 d 25000
5 e 15000

set serveroutput on;


declare
e_id number:=&e_id;
e_salary number;
--e_id employee.emp_id%type;
--e_salary employee.salary%type;
begin
select salary into e_salary from employee where emp_id=e_id;
dbms_output.put_line('salary of emp_id = ' ||e_id|| 'is' ||e_salary);
exception
when NO_DATA_FOUND then
dbms_output.put_line('employee id not found');
end;
/

Enter value for e_id: 6


old 2: e_id number:=&e_id;
new 2: e_id number:=6;
employee id not found

PL/SQL procedure successfully completed.


SYCM-Computer Technology Department, SVCP, Pune Page 28
Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

2) User defined exception:


These exceptions are defined by the user.
It must be declare by the user in the declaration section of the block .
User defined exception is declared by using following syntax:
Exception_name exception;
e.g: myex exception;

user defined exception is raised/called from begin section by using following


syntax:
raise exception_name;
e.g: raise myex;

example: pl/sql program of user defined exception:

set serveroutput on;


declare
n number:=&n;
myex exception; --(USER DEFINED EXCEPTION DECLARATION)
begin
if(n>1) then
dbms_output.put_line('value of n is greater than 1');
else
raise myex; --EXCEPTION RAISED
end if;
exception
when myex then --
dbms_output.put_line('n is not greater than 1');
end;
/

o/p
Enter value for n: -6
old 2: n number:=&n;
new 2: n number:=-6;
n is not greater than 1
PL/SQL procedure successfully completed.

SYCM-Computer Technology Department, SVCP, Pune Page 29


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

Above example will raise exception in else part when user enters value of n less
than 1.

Example. 2) PL/sql program that asks or customer_id ,when user enters


invalid id , the exception invalid_id is raised.
Consider,Customer: table

CUSTOMER_ID NAME CITY


----------- --------------- ---------------
1 a pune
2 b mumbai
3 c delhi
4 d goa
5 e khanapur

Ans:

DECLARE
c_id customer.id%type := &c_id;
c_name customer.Name%type;
c_addr customer.address%type;
invalid_id EXCEPTION; -- user defined exception
BEGIN
IF c_id <= 0 THEN
RAISE invalid_id;
ELSE
SELECT name, address INTO c_name, c_addr FROM customer WHERE
id = c_id;
DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
END IF;
EXCEPTION
WHEN invalid_id THEN
dbms_output.put_line('ID must be greater than zero!');
WHEN no_data_found THEN
dbms_output.put_line('No such customer!');
WHEN others THEN
dbms_output.put_line('Error!');
END;
/
SYCM-Computer Technology Department, SVCP, Pune Page 30
Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

o/p:

Enter value for cc_id: -3


old 2: c_id customer.id%type := &cc_id;
new 2: c_id customer.id%type := -3;
ID must be greater than zero!

4.4 Cursors:- Implicit and Explicit Cursors, Declaring,opening and closing


cursor, fetching a record from cursor,cursor for loops, parameterized cursors

(Explain cursor? List the two types of cursor in detail.)

Cursor:
A cursor is a temporary work area/context area created in the system memory
when a SQL statement is executed.
In PL/SQL, cursor has the attributes like ,
%FOUND,
%ISOPEN,
%NOTFOUND,
%ROWCOUNT. etc

Types of Cursor: 1.Implicit Cursor 2.Explicit Cursor

Implicit Cursor:
Implicit cursors are automatically created by Oracle whenever an SQL statement is
executed, when there is no explicit cursor for the statement.
Whenever a DML statement (INSERT, UPDATE and DELETE) is there, an
implicit cursor is associated with this statement.
For INSERT operations, the cursor holds the data that needs to be inserted.
For UPDATE and DELETE operations, the cursor identifies the rows that would
be affected.

Example of implicit cursor:


Consider table employee;

SYCM-Computer Technology Department, SVCP, Pune Page 31


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

EMPNO EMPNAME SALARY

---------- --------------- ----------

1 a 25000

2 b 30000

6 c 40000

10 d 30000

SQL> set serveroutput on;

SQL> begin

update employee set salary=salary+5000 where empno=&empno;

if sql%found then

dbms_output.put_line('table is modified');

else

dbms_output.put_line('table is not modified');

end if;

end;

Enter value for empno: 10

old 2: update employee23 set salary=salary+5000 where empno=&empno;

new 2: update employee23 set salary=salary+5000 where empno=10;

table is modified

PL/SQL procedure successfully completed.

Explicit cursor:

SYCM-Computer Technology Department, SVCP, Pune Page 32


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

Explicit cursors are programmer/user defined cursors for gaining more control over
the context area.
An explicit cursor should be defined in the declaration section of the PL/SQL
Block. It is created on a SELECT Statement which returns more than one row.

 explicit cursor involves four steps:

1) Declaring the cursor for initializing in the memory


Syntax: Cursor cursor_name IS select_statement;
e.g cursor c1 is select rollno,name,marks,dept from student
2) Opening the cursor for allocating memory
Syntax: Open cursorname;
e.g. Open c1;

3) Fetching the cursor for retrieving data


Syntax: Fetch cursorname INTO variable1,variable2…
e.g: fetch c1 into c_rollno,c_name,c_marks,c_dept;

4) Closing the cursor to release allocated memory


Syntax: Close cursorname;
e.g: Close c1;

example for explicit cursor::

Q. pl/sql program to display record of students using explicit cursor.


consider table student,
ROLLNO NAME MARKS DEPT
---------- -------------- ---------- ---------------
1 ram 90 computer
4 shyam 80 Ej
6 raj 95 IF
3 sam 98 computer
8 rudda 70 Civil

set serveroutput on;


declare
cursor c1 is select rollno,name,marks,dept from student;
c_rollno student.rollno%type;
c_name student.name%type;
c_marks student.marks%type;
SYCM-Computer Technology Department, SVCP, Pune Page 33
Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

c_dept student.dept%type;
begin
open c1;
loop
fetch c1 into c_rollno,c_name,c_marks,c_dept;
exit when c1%NOTFOUND;
dbms_output.put_line(c_rollno|| ' '||c_name|| ' ' ||c_marks|| ' ' ||c_dept);
end loop;
close c1;
end;
/

o/p
1 ram 90 computer
4 shyam 80 Ej
6 raj 95 IF
3 sam 98 computer
8 rudda 70 Civil

Example 2)
Q. pl/sql program to display record of students in computer department using

set serveroutput on;


declare
cursor c1 is select rollno,name,marks,dept from student where dept='computer';
c_rollno student.rollno%type;
c_name student.name%type;
c_marks student.marks%type;
c_dept student.dept%type;
begin
open c1;
loop
fetch c1 into c_rollno,c_name,c_marks,c_dept;
exit when c1%NOTFOUND;
dbms_output.put_line(c_rollno|| ' '||c_name|| ' ' ||c_marks|| ' ' ||c_dept);
end loop;
close c1;
end;
SYCM-Computer Technology Department, SVCP, Pune Page 34
Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

/
o/p:
1 ram 90 computer
3 sam 98 computer

Ex. 3) Pl/sql program to display the even number of record (i.e even position
number record) details of student

set serveroutput on;


declare
cursor c1 is select rollno,name,marks,dept from student ;
c_rollno student.rollno%type;
c_name student.name%type;
c_marks student.marks%type;
c_dept student.dept%type;
counter number:=0; -- to count record no.
begin
open c1;
loop
fetch c1 into c_rollno,c_name,c_marks,c_dept;
exit when c1%NOTFOUND;
counter:=counter+1; --to increment record no
if mod(counter,2)=0 then
dbms_output.put_line(c_rollno|| ' '||c_name|| ' ' ||c_marks|| ' ' ||c_dept);
end if;
end loop;
close c1;
end;
/

o/p
4 shyam 80 Ej
3 sam 98 computer

Ex 4. )PL/SQL program that displays the number of items having price more
than 10000

SYCM-Computer Technology Department, SVCP, Pune Page 35


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

Consider store table,

ITEM_ID NAME PRICE


---------- --------------- ----------
11 soap 9000
12 jwellary 10000
13 cloths 20000
14 Pen 8000
15 pencil 35000

set serveroutput on;


declare
cursor c1 is select item_id,name,price from store where price>10000;
c_item_id store.item_id%type;
c_name store.name%type;
c_price store.price%type;
counter number:=0; -- to count record no.
begin
open c1;
loop
fetch c1 into c_item_id,c_name,c_price;
exit when c1%NOTFOUND;
counter:=counter+1; --to increment record no
end loop;
close c1;
dbms_output.put_line('the number of records have price more than
10000:='||counter);
end;
/
o/p:

the number of records have price more than 10000:=2

Q. Write step by step syntax to create, open and close cursor in PL/SQL
block.
(Create - 1 Mark, Open - 1 Mark, Close - 1 Mark, Syntax - 1 Mark)
SYCM-Computer Technology Department, SVCP, Pune Page 36
Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

Ans: Steps in Cursor:


DECLARE
CURSOR cursor_name ISselect_statement;
OPEN cursor_name;
FETCH cursor_name into record_name;
PL/SQL block;
CLOSE cursor_name;

4.5 Procedures: - Advantages, Create, Execute and Delete a Stored Procedure

Procedure:
A procedure is also called as stored procedure.A procedure is named PL/SQL
block which perform one or more specifies task.

Advantages of Procedure:
1. Security: Procedures offer more security.
2. Productivity: Avoids redundant code for common procedures in multiple
applications.
3. Memory savings: Requires only one copy of the code for multiple users.
4. Performance: Precompiled code hence no compilation is required to execute a
code.
5. Integrity and accuracy: As procedure is needed to be tested only once hence
guarantee of accurate result.

Create procedure:
Syntax:
CREATE OR REPLACE PROCEDURE procedure_name (parameter_name1
mode datatype…parameter_name_n mode datatype)
IS/AS
Declaration section
BEGIN
Execution section
EXCEPTION
exception section
END;
/

Here, procedure_name is name of procedure


Parameter_name is name of variable or column

SYCM-Computer Technology Department, SVCP, Pune Page 37


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

mode is IN i.e read mode /OUT i.e write mode /IN and OUT i.e read and write
mode, datatype is the type data.

Note: Syntax of compilation and execution of procedure:

Compilation:
Show errors;

Execution/run:
Exec procedure_name(parameter_value1,parameter_value2,…);
OR
Execute procedure_name(parameter_value1,parameter_value2,…);

Example of procedure
Pl/sql procedure to Greet user by name.
set serveroutput on;

create or replace procedure greetings(name in varchar)

as

begin

dbms_output.put_line('Hello ' ||name|| ' welcome');

end;

o/p:

Procedure created.

SQL> show errors;

No errors.

SQL> exec greetings(‘sujata’);

Hello sujata welcome

SYCM-Computer Technology Department, SVCP, Pune Page 38


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

Imp:Example2: PL/SQL procedure to calculate factorial of a give number.

factorial of number by procedure


SQL> set serveroutput on;

SQL> create or replace procedure factorial(n in number)

as

fact number:=1;

i number;

begin

for i in 1..n

loop

fact:=fact*i;

end loop;

dbms_output.put_line('factorial='||fact);

end;

Procedure created.

SQL> show errors;

No errors.

SQL> exec factorial(4);

factorial=24

SYCM-Computer Technology Department, SVCP, Pune Page 39


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

PL/SQL procedure successfully completed.

Example 3. Pl/sql procedure to insert 3 records in employee table

create or replace procedure insert_records

as

begin

insert into emp values(6,'parth',33);

insert into emp values(7,'pari',44);

insert into emp values(8,'prisha',33);

commit;

dbms_output.put_line('3 records in emp table inserted successfully');

end;

o/p

exec insert_records;

3 records in emp table inserted successfully record inserted.

SYCM-Computer Technology Department, SVCP, Pune Page 40


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

Example 4. Pl/sql procedure to count number of records in department of


employee.

Consider emp table,


ENO ENAME DEPTNO
----- --------------- ----------
1 abc 11
2 xyz 22
3 pqr 11
4 lmn 11
5 rst 22
set serveroutput on;
create or replace procedure emp_count(dept_no in number)
as
counter number;
begin
select count(eno) into counter from emp where deptno=dept_no;
dbms_output.put_line('no of employees in department:'||counter);
end;
/
Procedure created.
o/p:
SQL> exec emp_count(11);
no of employees in department:3
PL/SQL procedure successfully completed.

SYCM-Computer Technology Department, SVCP, Pune Page 41


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

Delete procedure

Syntax

Drop procedure procedure_name;

e.g drop procedure factorial;

4.6 Functions :- Advantages, Create, Execute and Delete a Function

Definition: Function is a logically grouped set of SQL and Pl/SQL statements that
perform a specific task. function will return value.

Advantages of Function:
1. Code reusability feature can be used.
2. It saves time and cost.
3. Increases flexibility of the program.
4. Memory space required is less.
5. It can return value to the calling program.

Create Function:
Syntax:
CREATE OR REPLACE FUNCTION function_name(parameter_name1 mode
datatype…parameter_name_n mode datatype )
RETURN return_datatype
IS/AS
Declaration_section
BEGIN
Execution_section
Return return_variable;
EXCEPTION
exception section
Return return_variable;
END;

Here, function_name is name of function


Parameter_name is name of variable or column
mode is IN i.e read mode /OUT i.e write mode /IN and OUT i.e read and write
mode, datatype is the type data.

SYCM-Computer Technology Department, SVCP, Pune Page 42


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

Note: Syntax of compilation and execution


 Compilation:
 Show errors;
 Execution:

1)Var variable_name datatype;


Var f number;
2)Exec:variablename:=function_name(parameter_values);
exec:f:=fun_fact(4);
3)print variable_name;
print f;

 Example
write pl/sql function to calculate the factorial of gven no.

set serveroutput on;

create or replace function factorial(n in number) return number

as

fact number:=1;

i number;

begin

for i in 1..n

loop

fact:=fact*i;

end loop;

return fact;

end;

SYCM-Computer Technology Department, SVCP, Pune Page 43


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

Function created.

SQL> show errors;

No errors.

SQL> var f number;

SQL> exec:f:=factorial(4);

PL/SQL procedure successfully completed.

SQL> print f;

----------

24

Example 2). write pl/sql function which will compute and return the
maximum of two values.

set serveroutput on;

create or replace function max_fun(a in number, b in number) return number

as

begin

if a>b then

return a;

else

return b;

end if;

end;

SYCM-Computer Technology Department, SVCP, Pune Page 44


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

o/p:

Function created.
SQL> var max number;
SQL> exec:max:=max_fun(14,50);
PL/SQL procedure successfully completed.
SQL> print max;
MAX
----------
50
Example 3) Pass empno as a parameter to function and write a function to
return salary of that
employee.

SQL> set serveroutput on;

SQL> create or replace function emp_fun(empno in number) return number

as

salary number:=50000;

begin

return salary;

end;

Function created.

SQL> show errors;

No errors.

SYCM-Computer Technology Department, SVCP, Pune Page 45


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

SQL> var s number;

SQL> exec:s:=emp_fun(11);

PL/SQL procedure successfully completed.

SQL> print s;

----------

50000

Delete Function
by using drop function we can delete function permenantly.
Syntax:
drop function function_name;
e.g: drop function max_fun;

4.7 Database Triggers: - Use of Database Triggers, Types of Triggers, Create


trigger, delete trigger

Trigger:

A trigger is a PL/SQL block structure which is fired when DML statements like
Insert, Delete, Update is executed on a database table. A trigger is triggered
automatically when an associated DML statement is executed.
Trigger is an alternative for the integrity constraint.
:new and :old these two attributes are used in trigger

:new is used for insert ,delete

:old is used for delete

SYCM-Computer Technology Department, SVCP, Pune Page 46


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

It is raised explicitly in sequence of statements in execution section (begin)using:


Raise_application_error(Exception_Number,Error_Message);
e.g: raise_application_error(-20343,'salary must be greater than 5000');

Use of database trigger:


• Triggers can be used to prevent invalid transaction and to apply complex security
authorization.
• It can be used to implement complicated integrity constraints.
• It can be used to maintain duplicate table and to check the data modifications

Types of Triggers:

(Q .list types of triggers)

1. Statement-level trigger
2. Row-level trigger
3. Before-trigger
4. After-trigger

Create Trigger

Syntax
CREATE OR REPLACE TRIGGER trigger name
BEFORE / AFTER
INSERT / UPDATE / DELETE
ON Table_name
FOR EACH ROW/ FOR EACH STATEMENT
WHEN CONDITION
BEGIN
Execution_section
Return return_variable;
EXCEPTION
exception section
Return return_variable;
END;
/
Where,

Here, trigger_name is name of trigger


Befor/after/for each row/for each statement are the types of trigger

SYCM-Computer Technology Department, SVCP, Pune Page 47


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

Insert/update/delete are the dml statements to be fired


Table_name is name of table.

Example:
create a trigger on emp table which invoked when salary is below 5000(while
insert).

set serveroutput on;

create or replace trigger trg1

before insert

on emp

for each row

begin

if :new.salary<5000 then

raise_application_error(-20343,'salary must be greater than 5000');

end if;

end;

(for above example consider table emp(eno,ename,salary)

Output:

SQL> insert into emp values(12,'abhi',0);

insert into emp values(12,'abhi',0)

ERROR at line 1:

ORA-20343: Salary should be greater than 0

ORA-06512: at "SYSTEM.TRG1", line 3

SYCM-Computer Technology Department, SVCP, Pune Page 48


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

ORA-04088: error during execution of trigger 'SYSTEM.TRG1'

Example2. create a trigger which invokes on updatin of record in department


table.

create table department,

DEPTNO DEPTNAME SALARY

---------- --------------- ----------

11 design 23000

22 development 15000

33 sales 20000

set serveroutput on;

create or replace trigger trg22

before update

on department

for each row

begin

if :new.salary<5000 then

raise_application_error(-20343,'salary must be greater than 5000');

end if;

end;

Trigger created.

SYCM-Computer Technology Department, SVCP, Pune Page 49


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

SQL> update department set salary=2000 where deptno=22;

update department set salary=2000 where deptno=22

ERROR at line 1:

ORA-20343: salary must be greater than 5000

ORA-06512: at "SYSTEM.TRG22", line 3

ORA-04088: error during execution of trigger 'SYSTEM.TRG22'

Example 3) create trigger which invokes for deletion of employee record

set serveroutput on;

create or replace trigger trg33

before delete

on emp

for each row

begin

if :old.eno<5 then

raise_application_error(-20343,'eno must be greater than 5');

end if;

end;

o/p

Trigger created.
SYCM-Computer Technology Department, SVCP, Pune Page 50
Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

SQL> delete from emp where eno=2;

delete from emp where eno=2;

ERROR at line 1:

ORA-20343: eno must be greater than 5

ORA-06512: at "SYSTEM.TRG33", line 3

ORA-04088: error during execution of trigger 'SYSTEM.TRG33'

Advantages of triggers:

1. Triggers provide an alternative way to check the integrity of data.


2. Can catch errors in business logic in the database layer.
3. SQL triggers provide an alternative way to run scheduled tasks.
4. Triggers are very useful to audit the changes of data in tables.

Difference between trigger and procedure

SYCM-Computer Technology Department, SVCP, Pune Page 51


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

Differentiate between function and procedure.


(Any four point – 1Mark each)

PREVIOUS MSBTE PL/SQL PROGRAMS


write a pl/sql program to print numbers from 50 to 60 using
for loop.
(note: any other logic also considered)
declare
x number;
begin
for x in 50 . . 60
loop
dbms_output.put_line(x);
end loop;
end;
/
Q.greatest number among three number
Using if else if then
Ans:
declare
a number:=&a;
b number:=&b;
c number:=&c;
begin

SYCM-Computer Technology Department, SVCP, Pune Page 52


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

if(a>b) and (a>c) then


dbms_output.put_line(‘a is greater’);
else if (b>a) and(b>c) then
dbms_output.put_line(‘b is greater’)
else
dbms_output.put_line(‘c is greater’);
end if;

SYCM-Computer Technology Department, SVCP, Pune Page 53


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

end if;
end;
/
Or using nested if
declare
a number:=&a;
b number:=&b;
c number:=&c;
begin
if(a>b) then
if(a>c) then
dbms_output.put_line(‘a is greater’);
else
dbms_output.put_line(‘c is greater’);
else if(b>c)
dbms_output.put_line(‘b is greater’)
else
dbms_utput.put_line(‘c is greater’)
end if;
end if;
end if;
end;
/

SYCM-Computer Technology Department, SVCP, Pune Page 54


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

write pl/sql procedure to calculate factorial of a give number.


(correct program – 4marks)
[note: any other relevant logic shall be considered]
ans:
sql> create or replace procedure factorial(n in number)
is
fact number :=1;
begin
for i in 1..n
loop
fact :=fact * i;
end loop;
dbms_output.put_line(fact);
end;
o/p:procedure created
sql>
factorial(5);
Q. Write PL/SQL function to calculate factorial of a give number.
create or replace function fun_Factorial(n in number) return number as
fact number:=1;
i number;
begin
for i in 1..n
loop
fact:=fact*i;
end loop;
return fact;
end;
/

SYCM-Computer Technology Department, SVCP, Pune Page 55


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

2) Pass empno as a parameter to function and write a function to return


salary of that
employee.
create or replace function fun_employee(empno in number) return number as
salary number:=50000;
begin
return salary;
end;
/
write pl/sql program to display square of any number.
(for correct program - 4 marks)
ans: declare
no number:=&no;
sqr number:=1;
begin
sqr:=no*no;
dbms_output.put_line(‘square of a number’ ||no||’is’||sqr);
end;
/
write a pl/sql program using while loop to display n even numbers.
(correct logic - 2 marks; correct syntax - 2 marks)
ans: set serveroutput on;
declare
n number:=&n;
i number:= 0;
begin
for i in 1 . . n
loop
if( mod (i,2)=0) then
dbms_output.put_line(i);
end if;
end loop;
end;
/

SYCM-Computer Technology Department, SVCP, Pune Page 56


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

q.write pl/sql program to reverse a number (e.g if number is 123 the reverse
of it is 321)
ans:
declare
n number:=&n;
rev number:=0;
rem number:=0;
begin
while(n>0)
loop
rem=mod(n,10);
rev:=rev*10+rem;
n:=n/10;
end loop;
dbms_output.put_line(rev);
end;
/
Q. Write Pl/SQL program to handle zero_divide exception.
declare
a number:=20;
b number:=0;
c number;
begin
dbms_output.put_line(a);
dbms_output.put_line(b);
c:= a / b; --raise exception
dbms_output.put_line(c); -- result will not displayed due to exception is raised
implicitly as b is 0
exception
when zero_divide then
dbms_output.put_line(‘trying to divide by zero ‘);
end;
/

SYCM-Computer Technology Department, SVCP, Pune Page 57


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

Q. Write a trigger which invokes on deletion of record on emp table.


create or replace trigger trg1
before
delete
on emp
for each row
begin
if :old.empno<=0 then
rasie_application_error(-20343,‘employee record not found to delete’);
end if;
end;
/
write a pl/sql program to print even or odd number from given range (accept
number range from user).
declare
a number :=&a;
b number :=&b;
c number :=&c;
begin
if(c=1) then
for i in a..b loop
if(mod(i,2)=0) then
dbms_output.put_line(i);
end if;
end loop;
else
for i in a..b loop
if(mod(i,2)=1) then
dbms_output.put_line(i);
end if;
end loop;
end if;
end;
/

SYCM-Computer Technology Department, SVCP, Pune Page 58


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

write a pl/sql program to find the square of a number given by user


usingwhile….loop.(accept the number from user dynamically)
(correct program - 4 marks) [**note: any relevant program logic shall be
considered**]
ans:
set serveroutput on;
declare
n number:=&n;
sqr number:= 0;
n_cntr number:=0;
begin
dbms_output.put_line(n);
while n_cntr< n loop
sqr:= sqr + n;
n_cntr := n_cntr + 1;
end loop;
dbms_output.put_line('square of ' || n || ' is ' || sqr);
end;
/

PREVIOUS MSBTE QUESTIONS


Question Exam Marks
State any four PL/SQL datatypes. S-19 2M
Explain PL/SQL block structure with the help of diagram. S-19 4M
Explain block structure of PL/SQL S-22 4M
Draw the block structure of PL-SQL. S-23 2M
state two advantages of PLISQ W-22 2M
List two advantages of PL/SQL. S-22 2M
Explain exception handling in PL/SQL with example. S-19 4M
Explain Exception handling with it’s types. W-19 4M
Describe exception handling in brief. W-18 4M
Explain cursor with example. S-19 4M
Explain implicit and explicit cursors. W-19 4M
Define cursor. List the two types of cursor. W-19 2M
Define cursor. Enlist the types of cursors. W-23 2M
Write step by step syntax to create, open and close cursor in PL/SQL. W-18 4M
Write and explain syntax to create, open and close cursor in PL/SQL S-24 4M
Explain the steps of cursor implementation with syntax and example. W-22 4M
Write step by step syntax to create, open and close cursor in PL/SQL S-23 4M
Explain cursor with syntax for declaring, opening and closing a cursor. W-23 4M
State the use of database trigger and also list types of trigger. S-19 4M
Explain use of database trigger. List types of trigger. Write command to create and S-24 6M

SYCM-Computer Technology Department, SVCP, Pune Page 59


Unit 4. PL/SQL Programming Ms.S.A.Kshirsagar

delete trigger.
Write and Explain the syntax for creating database trigger. W-22 4M
Explain trigger with suitable example S-23 4M
Define database trigger. How to create and delete trigger ? W-19 4M
Write and explain syntax for creating Trigger. S-22 4M
Write a PL/SQL program to calculate factorial of a given number. S-19 6M
State any two advantages of functions in PL/SQL. W-19 2M
Explain any one control structure in PL/SQL with example. w-19 4M
Explain conditional control in PL/SQL with example. S-22 4M
Write a PL/SQL program to print n even numbers using For Loop. W-19 6M
Draw the block structure of PL/SQL . List advantages of PL/SQL. W-18 4M
Write a PL/SQL program which accept the customer ID from the user if user enters W-18 6M
an invalid ID then the exception invalid_id is raised using exception handling(
List two advantages of PL/SQL. S-22 2M
Write a PL/SQL program, which accept the number from user. If user enters an odd S-22 6M
number then exception invalid number is raised using user defined exception
handling
Differentiate between PL/SQL procedure and function with syntax and example. S-24 6M
Write PL/SQL program to print number of employee working in specified S-24 4M
department consider emp table and accept dept-no. from user
Write PL/SQL code to print largest number from three numbers. S-24 4M
Write a PLISQL code to print reverse of a number. W-22 4M
Write a trigger which invokes on deletion of record on emp table. W-22 4M
Write a PLISQL code to check whether specified employee is present in Emp table W-22 6M
or not. Accept empno from user. If employee does not exist display message using
exception handling.
State syntax of while loop command. S-23 2M
Write a PL-SQL program to print odd numbers from 1 to 10. S-23 4M
Explain for loop syntax in PL-SQL with example of printing 10 to 1 reverse S-23 6M
numbers.
Explain function in PL-SQL with suitable example S-23 6M
Explain function in PL/SQL with example. W-18 4M
Explain procedures in PL/SQL with example. W-23 4M
Compare database triggers and procedures. (Write four differences) And explain W-23 4M
the use of database trigger
Write a PL/SQL program to print sum of n odd numbers using for loop. W-23 6M

SYCM-Computer Technology Department, SVCP, Pune Page 60

You might also like