CH 4 PL SQL Programming
CH 4 PL SQL Programming
PL/SQL is the procedural extension to SQL with design features of programming languages.
Data manipulation and query statements of SQL are included within procedural units of code.
PL/SQL is a combination of SQL along with the procedural features of programming languages.
It was developed by Oracle Corporation in the early 90’s to enhance the capabilities of SQL.
Oracle uses a PL/SQL engine to processes the PL/SQL statements. A PL/SQL language code can
be stored in the client system (client-side) or in the database (server-side).
1. PL/SQL Block
2. PL/SQL Engine
3. Database Server
PL/SQL block:
PL/SQL Engine
PL/SQL engine is the component where the actual processing of the codes takes place.
PL/SQL engine separates PL/SQL units and SQL part in the input (as shown in the image below).
The separated PL/SQL units will be handled by the PL/SQL engine itself.
The SQL part will be sent to database server where the actual interaction with database takes place.
It can be installed in both database server and in the application server.
Database Server:
This is the most important component of Pl/SQL unit which stores the data.
The PL/SQL engine uses the SQL from PL/SQL units to interact with the database server.
It consists of SQL executor which parses the input SQL statements and execute the same.
Advantages of PL/SQL
Block Structures: PL SQL consists of blocks of code, which can be nested within each other. Each
block forms a unit of a task or a logical module. PL/SQL Blocks can be stored in the database and
reused.
Procedural Language Capability: PL SQL consists of procedural language constructs such as conditional
statements (if else statements) and loops like (FOR loops).
Better Performance: PL SQL engine processes multiple SQL statements simultaneously as a single
block, thereby reducing network traffic.
Error Handling: PL/SQL handles errors or exceptions effectively during the execution of a
PL/SQL program. Once an exception is caught, specific actions can be taken depending upon the
type of the exception or it can be displayed to the user with a message.
SQL PL/SQL
PL/SQL is a block of codes that used to
SQL is a single query that is used to
write the entire program blocks/
perform DML and DDL operations.
procedure/ function, etc.
DECLARE
Variable declaration
BEGIN
Program Execution
EXCEPTION
Exception handling
END;
1. Declaration Section:
The Declaration section of a PL/SQL Block starts with the reserved keyword DECLARE.
This section is optional and is used to declare any placeholders like variables, constants, records and
cursors, which are used to manipulate data in the execution section.
Cursors are also declared in this section.
2. Execution Section:
The Execution section of a PL/SQL Block starts with the reserved keyword BEGIN and ends with END.
This is a mandatory section and is the section where the program logic is written to perform any task.
The programmatic constructs like loops, conditional statement and SQL statements form the part of
execution section.
3. Exception Section:
The Exception section of a PL/SQL Block starts with the reserved keyword EXCEPTION.
This section is optional.
Any errors in the program can be handled in this section, so that the PL/SQL Blocks terminates gracefully.
Note:- Every statement in the above three sections must end with a semicolon ;
1. Anonymous blocks
2. Named Blocks
Anonymous blocks:
Anonymous blocks are PL/SQL blocks which do not have any names assigned to them. They need to be created
and used in the same session because they will not be stored in the server as database objects.
Named blocks:
Named blocks have a specific and unique name for them. They are stored as the database objects in the server.
The block structure is same as an anonymous block, except it will never start with the keyword
‘DECLARE’. Instead, it will start with the keyword ‘CREATE’ which instruct the compiler to create it as
a database object.
Named blocks are basically of two types:
1. Procedure
2. Function
Category Description
Scalar Single values with no internal components, such as a NUMBER, DATE, or
BOOLEAN.
Large Object Pointers to large objects that are stored separately from other data items, such as text,
(LOB) graphic images, video clips, and sound waveforms.
Composite Data items that have internal components that can be accessed individually. For
example, collections and records.
Reference Pointers to other data items.
Syntax:
<variable name> <datatvpe>;
• For example:
1. dept varchar2(10) NOT NULL := “HR Dept”;
2. Sal number:=10000;
Example:
DECLARE
a integer := 10;
b integer := 20;
c integer;
f real;
BEGIN
c := a + b;
dbms_output.put_line('Value of c: ' || c);
f := 70.0/3.0;
dbms_output.put_line('Value of f: ' || f);
END;
/
When the above code is executed, it produces the following result −
Value of c: 30
Value of f: 23.333333333333333333
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:
o Local Variable: Local variables are the inner block variables which are not accessible to outer blocks.
o Global Variable: Global variables are declared in outermost block.
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;
After the execution, this will produce the following result:
Variable Attributes:
PL/SQL provides the facility to declare a variable without having to specify a particular data type using %TYPE
attributes.
%TYPE:
The %TYPE attribute is used to declare variables according to the already declared variable or database column.
It is used when you are declaring an individual variable, not a record. The data type and precision of the variable
declared using %TYPE attribute is the same as that of the column that is referred from a given table.
Syntax :
<var_name> <tab_name>.<column_name>%TYPE;
Consider a declaration.
This declaration will declare a variable SALARY that has the same data type as column SAL of the EMP table.
Example:
DECLARE
SALARY EMP.SAL % TYPE;
ECODE EMP.empno % TYPE;
BEGIN
Ecode :=:Ecode;
Select SAL into SALARY from EMP where EMPNO = ECODE;
dbms_output.put_line('Salary of ' || ECODE || 'is = || salary');
END;
Result:
• A constant is a value used in a PL/SQL Block that remains unchanged throughout the program.
• General Syntax to declare a constant is:
Constant_name CONSTANT datatype: = VALUE;
o Constant_name:it is the name of constant just like variable name. The constant word is a reserved word
and its value does not change.
o VALUE: it is a value which is assigned to a constant when it is declared. It can not be assigned later.
• The programming constructs are similar to how you use in programming languages like Java and C++.
1. IF-THEN Statement:-
• The IF statement associates a condition with a sequence of statements enclosed by the keywords THEN
and END IF.
• If the condition is true, the statements get executed and if the condition is false or NULL then the
IF statement does nothing.
Syntax : Example:-
IF <condition> THEN IF (a<=20) THEN
Statement1; c:=c+1;
END IF; END IF;
• Flow Diagram
Example 1:-
2. IF-THEN-ELSE Statement:-
Syntax:
IF <condition> THEN
Statement1;
ELSE
Statement2;
END IF;
Example
IF color = red THEN
dbms_output.put_line (‘you have chosen a red car');
ELSE
dbms_output.put_line (‘please choose a color for your
car');
END IF;
Flow Diagram
Example 1:-
SET SERVEROUTPUT ON;
DECLARE
a number(3) := 100;
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;
/
Output:-
a is not less than 20
value of a is : 100
3. IF-THEN-ELSEIF Statement:-
• The IF-THEN-ELSIF statement allows you to choose between several alternatives. An IF-THEN
statement can be followed by an optional ELSIF...ELSE statement. The ELSIF clause lets you add
additional conditions.
• Syntax for IF-THEN-ELSIF statement
IF (boolean_expression 1) THEN
S1; - - Executes when the Boolean expression 1 is true
ELSIF (boolean_expression 2) THEN
S2; - - Executes when the Boolean expression 2 is true
ELSIF (boolean_expression 3) THEN
S3; - - Executes when the Boolean expression 3 is true
ELSE
S4; -- executes when the none of the above condition is true
END IF;
• For Example 1
DECLARE
a number(3);
b number(3);
BEGIN
a:=:a;
b:=:b;
IF (a>b) THEN
dbms_output.put_line('A is Largest' );
ELSE
dbms_output.put_line(' B is Largest' );
END IF;
END;
DECLARE
a number(3);
b number(3);
c number(3);
BEGIN
a:=:a;
b:=:b;
c:=:c;
IF (a>b) and (a>c) THEN
dbms_output.put_line('A is maximum' );
ELSIF ( b>a) and (b>c) THEN
dbms_output.put_line(' B is maximum' );
ELSE
dbms_output.put_line(' C is maximum' );
END IF;
END;
CASE statement: -
Syntax
CASE [expression]
WHEN condition_1 THEN result_1;
WHEN condition_2 THEN result_2;
WHEN condition_n THEN result_N;
……………
ELSE result - - default case
END CASE;
Flow Diagram
• A loop statement allows us to execute a statement or group of statements multiple times and following
is the general form of a loop statement in most of the programming languages −
1. Basic or simple LOOP: -
Basic loop structure encloses sequence of statements in between the LOOP and END LOOP statements.
Syntax:-
LOOP
Sequence of statements;
END LOOP;
Example
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.
Syntax of basic loop:
Example of PL/SQL EXIT Loop
LOOP
DECLARE
Sequence of statements; i NUMBER := 1;
BEGIN
END LOOP;
LOOP
EXIT WHEN i>10;
DBMS_OUTPUT.PUT_LINE(i);
Syntax of exit loop:
i := i+1;
END LOOP;
LOOP
END;
statements;
After the execution , you will get the following result:
EXIT;
{or EXIT WHEN condition;}
1
END LOOP;
2
3
o Initialize a variable before the loop body 4
o Increment the variable in the loop. 5
o You should use EXIT WHEN statement to exit 6
from the Loop. Otherwise the EXIT statement 7
without WHEN condition, the statements in the 8
Loop is executed only once. 9
10
2. While Loop: -
A WHILE LOOP statement in PL/SQL programming language repeatedly executes a target statement
as long as a given condition is true.
Syntax
WHILE condition LOOP
sequence_of_statements;
END LOOP;
Example
By default, iteration proceeds from the initial value to the final value, generally upward from the lower bound to
the higher bound.
•You can reverse this order by using the REVERSEkeyword.
•In such case, iteration proceeds the other way. After each iteration, the loop counter is decremented.
•However, you must write the range bounds in ascending (not descending) order.
Output:
DECLARE
VAR1 NUMBER; 100
BEGIN 90
VAR1:=10; 80
FOR VAR2 IN REVERSE 1..10 LOOP 70
DBMS_OUTPUT.PUT_LINE (VAR 60
1*VAR2); 50
END LOOP; 40
END; 30
20
10
Sample Program:-
1. Write a PL/SQL program to print even or odd number from given range (Accept number range
from user).
Ans: - NOTE: - In above program it specified that given range so that we used FOR LOOP
PL/SQL code to display EVEN numbers PL/SQL code to display ODD numbers
DECLARE
f number: =1;
n number := :n;
i number;
BEGIN
FOR i
IN 1..n
LOOP
f := f *
i;
END LOOP;
dbms_output.put_line (f);
END;
3. Write a PL/SQL program to find the square of a number given by user using WHILE….LOOP.
(accept the number from user dynamically)
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;
The continue statement is used to exit the loop from the reminder if its body either conditionally or
unconditionally and forces the next iteration of the loop to take place, skipping any codes in between.
The continue statement is not a keyword in Oracle 10g. It is a new feature encorporated in oracle 11g.
For example: If a continue statement exits a cursor FOR LOOP prematurely then it exits an inner loop and
transfer control to the next iteration of an outer loop, the cursor closes (in this context, CONTINUE works like
GOTO).
Syntax:
continue;
Example :
BEGIN
FOR i IN 1 .. 5 LOOP
IF i = 3 THEN
CONTINUE;
END IF;
DBMS_OUTPUT.PUT_LINE('Iteration # ' || i);
END LOOP;
END;
Output:
Iteration # 1
Iteration # 2
Iteration # 4
Iteration # 5
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.
Here the label declaration which contains the label_name encapsulated within the << >> symbol and must be
followed by at least one statement to execute.
Syntax:
GOTO label_name;
Here the label declaration which contains the label_name encapsulated within the << >> symbol and must be
followed by at least one statement to execute.
GOTO label_name;
..
..
<<label_name>>
Statement;
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;
/
value of a: 30
value of a: 31
value of a: 32
value of a: 33
value of a: 34
value of a: 36
value of a: 37
value of a: 38
value of a: 39
value of a: 40
value of a: 41
value of a: 42
value of a: 43
value of a: 44
value of a: 45
value of a: 46
value of a: 47
value of a: 48
value of a: 49
Statement processed.
Restriction on GOTO statement
o Cannot transfer control into an IF statement, CASE statement, LOOP statement or sub-block.
o Cannot transfer control from one IF statement clause to another or from one CASE statement WHEN
clause to another.
o Cannot transfer control from an outer block into a sub-block.
o Cannot transfer control out of a subprogram.
o Cannot transfer control into an exception handler.
Errors:-
Two types of errors can be found in a program: compilation errors and runtime errors.
There is a special section in a PL/SQL block that handles the runtime errors.
This section is called the exception-handling section, and in it, runtime errors are referred to as
exceptions.
The exception-handling section allows programmers to specify what actions should be taken when a
specific exception occurs.
Exception:-
In order to handle run time errors in the program, an exception handler must be added.
Exception handling is nothing but a code block in memory that will attempt to resolve current error
condition.
The exception-handling section has the following structure:
EXCEPTION
WHEN Exception_name THEN
Error-processing Statements;
Note: - The exception-handling section is placed after the executable section of the block.
DECLARE
--Declaration section
BEGIN
-- Program section
--Exception section
EXCEPTION
WHEN ex_name1 THEN
--Error handling statements
WHEN ex_name2 THEN
--Error handling statements
END;
Exception Handling Example:
DECLARE Output:-
num1 number := &sv_num1;
num2 number := &sv_num2; Enter value for sv_num1: 4
result number; old 2: v_num1 integer := &sv_num1;
BEGIN new 2: v_num1 integer := 4;
result := num1 / num2;
DBMS_OUTPUT.PUT_LINE ( ‘the is result: ’|| result); Enter value for sv_num2: 0
EXCEPTION old 3: v_num2 integer := &sv_num2;
WHEN ZERO_DIVIDE new 3: v_num2 integer := 0;
THEN
DBMS_OUTPUT.PUT_LINE (‘A number cannot be divided A number cannot be divided by zero.
by zero.’);
END; PL/SQL procedure successfully completed.
/
a) Predefined Exceptions:-
System exceptions are automatically raised by Oracle, when a program violates a RDBMS rule.
There are some system exceptions which are raised frequently, so they are pre-defined and given
a name in Oracle which are known as Named System Exceptions.
For example: NO_DATA_FOUND and ZERO_DIVIDE are called Named System exceptions.
Named system exceptions are:
1) not declared explicitly,
2) Raised implicitly when a predefined Oracle error occurs.
3) Caught by referencing the standard name within an exception-handling routine.
Oracle
Exception Description
Error
b) User-defined Exceptions
This type of an exception is called a user-defined exception because it is defined by the programmer.
Before the exception can be used, it must be declared.
It must be declare by the user in the declaration part of the block where the exception is used.
A user-defined exception is declared in the declarative part of a PL/SQL block as shown below:
DECLARE
Exception_name EXCEPTION;
Once an exception has been declared, the executable statements associated with this exception
are specified in the exception-handling section of the block.
User-defined Exceptions Example
DECLARE
E_invalid_id EXCEPTION;
BEGIN
………
EXCEPTION
WHEN E_invalid_id THEN
Dbms_output.Put_line (‘An Id Cannot Be Negative‘ );
END;
/
c) Raising Exceptions
Exceptions are raised by the database server automatically whenever there is any internal database
errors, but exceptions can be raised explicitly by the programmer by using the command RAISE.
Example
DECLARE
Exception_name EXCEPTION
BEGIN
IF (condition) THEN
RAISE Exception_name;
END IF;
EXCEPTION
WHEN Exception_name THEN
dbms_output.put_line (' Raising Exceptions ');
END;
4.5 Cursor
• A cursor is a temporary work area created in the system memory when a SQL statement is executed.
• A cursor contains information on a select statement and the rows of data accessed by it.
• This temporary work area is used to store the data retrieved from the database, and manipulate this
data.
• A cursor can hold more than one row, but can process only one row at a time. The set of rows the
cursor holds is called the active set.
• A cursor is a pointer to this context area.
• PL/SQL controls the context area through a cursor.
1. Implicit cursors
If database engine opens a cursor for internal processing, it is called as implicit cursor.
These are created by default when DML statements like, INSERT, UPDATE, and DELETE
statements are executed. They are also created when a SELECT statement that returns just one row is
executed.
When you execute DML statements like INSERT, UPDATE, DELETE & SELECT statements,
implicit statements are created to process these statements.
Oracle provides few attributes called as implicit cursor attributes to check the status of DML
operations.
The cursor attributes available are:-
%FOUND The return value is TRUE at least one row was SQL%FOUND
processed.
%ISOPEN True if cursor is open or false if cursor has not been SQL%ISOPEN
opened or has been closed.
DECLARE
total_rows number (2);
BEGIN
Update EMP set salary= salary +1500 where empno =10;
If SQL%FOUND then
Dbms_out.put_line (‘Emp table modified’);
Else
Dbms_out.put_line (‘Emp table not modified’);
End if;
END;
2. Explicit Cursors
• A user can open a cursor for processing data as required. Such user defined cursors are known
as explicit cursors.
• It should be defined in the declaration section of the PL/SQL block.
• They must be created when you are executing a SELECT statement that returns more than one row.
• Both implicit and explicit cursors have the same functionality, but they differ in the way they
are accessed.
• General Syntax for creating a cursor is as given below:
Where,
• cursor_name – A suitable name for the cursor.
• select_statement – A select query which returns multiple rows.
Step1: DECLARE the cursor: - Define cursor with a name & the associated SELECT statement.
• For
CURSOR c_customers IS SELECT id, name, address FROM
example:- customers;
• Step2: Opening the cursor in the Execution Section.
• For example:-
OPEN c_customers;
• Step3: FETCH the data from cursor into PL/SQL variables or records in the Execution Section.
• For example:-
FETCH c_customers INTO C_id, C_name, C_ address;
• Step4: CLOSE the cursor in the Execution Section before you end the PL/SQL Block.
• For example:-
CLOSE c_customers;
DECLARE
emp_rec emp_table %rowtype;
CURSOR emp_cur IS SELECT *FROM WHERE salary > 10K;
BEGIN
OPEN emp_cur;
FETCH emp_cur INTO emp_rec;
dbms_output.put_line (emp_rec.first_name || ' ' || emp_rec.last_name);
CLOSE emp_cur;
END;
4.6 Procedures
• A stored procedure or in simple a proc is a named PL/SQL block which performs one or more
specific task. This is similar to a procedure in other programming languages.
• A procedure has a header and a body.
• The header consists of the name of the procedure and the parameters or variables passed to the
procedure.
• The body consists or declaration section, execution section and exception section similar to a general
PL/SQL Block.
• A procedure is a module performing one or more actions; it does not need to return any values.
• The syntax for creating a procedure is as follows:
CREATE [OR REPLACE] PROCEDURE
procedure_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
{IS | AS}
BEGIN < procedure_body >
END procedure_name;
• Where,
• Example
Output:-
SQL> Procedure Created.
• Example of Procedures (Write PL/SQL Program to finds the minimum number of two values)
DECLARE
a number;
b number;
c number;
PROCEDURE findMin(x IN number, y IN number, z OUT number)
IS
BEGIN
IF x < y THEN
z: = x;
ELSE
z:= y;
END IF;
END;
BEGIN
a:= 23;
b:= 45;
FindMin (a, b, c);
dbms_output.put_line (' minimum of (23, 45): ' || c);
END;
/
4.7 Functions
• Functions are a type of stored code and are very similar to procedures.
• The significant difference is that a function is a PL/SQL block that returns a single value.
• Functions can accept one, many, or no parameters, but a function must have a return clause in the
executable section of the function.
• The datatype of the return value must be declared in the header of the function.
• A function has output that needs to be assigned to a variable, or it can be used in a SELECT statement.
• The syntax for creating a function is as follows:
• Functions Example
Table 1.
• When the above code is executed using the SQL prompt, it will produce the following result −
DECLARE
c number(2);
BEGIN
c := totalCustomers(); -- function call
dbms_output.put_line ('Total no. of Customers: ' ||
c); END;
/
• When the above code is executed at the SQL prompt, it produces the following result –
Output:-
SQL> Total no. of Customers: 5
PL/SQL procedure successfully completed.
• Example of Function
DECLARE
a number;
b number;
c number;
FUNCTION findMax(x IN number, y IN number)
RETURN number
IS
z number;
BEGIN
IF x > y
THEN z:= x;
ELSE
z:= y;
END IF;
RETURN z;
END;
BEGIN a:=
23; b:=
45;
c := findMax(a, b);
dbms_output.put_line(' Maximum of (23,45): ' ||
c); END;
/
Output
SQL> Maximum of (23, 45): 45
PL/SQL procedure successfully completed.
Deleting a procedures:-
• PL/SQL procedure is remove from the database by using the drop procedure command.
• Syntax:- DROP PROCEDURE procedure_name;
• Example:- DROP PROCEDURE greetings
Deleting a Function:-
• PL/SQL procedure is remove from the database by using the drop function command.
Procedure Function
• Triggers are stored programs, which are automatically executed or fired when some events occur.
• A trigger is a PL/SQL block structure which is fired when a DML statements like Insert, Delete, and
Update is executed on a database table.
• Triggers are, in fact, written to be executed in response to any of the following events −
• A database manipulation (DML) statement (DELETE, INSERT, or UPDATE)
• A database definition (DDL) statement (CREATE, ALTER, or DROP).
• A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or SHUTDOWN).
Where,
Trigger Example
• The following program creates a row-level trigger for the customers table that would fire for
INSERT or UPDATE or DELETE operations performed on the CUSTOMERS table. (referred
above table 1)
Triggering a Trigger
• Let us perform some DML operations on the CUSTOMERS table. Here is one INSERT
statement, which will create a new record in the table −
SQL> INSERT INTO CUSTOMERS VALUES (7, 'Kriti', 22, 'HP', 7500.00);
Old salary:
New salary: 7500
Salary difference:
1. Give any four advantages of using PL/SQL. (Any four advantages - 1 mark each)
2. What are Predefined exceptions and User defined exceptions? (Predefined exception - 2 marks; User
Defined exception - 2 marks)
3. Write a PL/SQL program to find the square of a number given by user using WHILE….LOOP.(accept the
number from user dynamically) (Correct Program - 4 marks)
4. Write a PL/SQL program using while loop to display n even numbers. (Correct logic - 2 marks; correct
syntax - 2 marks)
5. List out any four statements of PL/SQL. (Any four statement list/syntax - 1 mark each)
6. What is database trigger? Compare database triggers and procedures and explain the use of database
trigger. (Definition - 1 mark; Comparison - 2 marks; Uses - 1 mark)
7. Explain PL/SQL block structure. (Correct Explanation - 4 marks)
8. List types of cursor and explain each with example. (Description - 1 mark; example - 1 mark for each
type; 1 mark shall be awarded if only list is given)
WINTER– 16
1. Define cursor? List the two types of cursor.
2. Explain the exception handling with its two types.
3. Explain PL/SQL Block structure.
4. What is database Trigger? How to create Trigger?
5. Write a PL/SQL program to print even or odd number from given range (Accept number range from user).
6. Explain function in PL/SQL with suitable example
7. Explain loop control structure used in PL/SQL.
WINTER – 15
1. What statement is used in PL/SQL to display the output? (Correct statement – 2
Marks) Ans:
dbms_output.put_line (var/msg);
OR
set serveroutput on;
dbms_output.put_line (var/msg);
2. What is Cursor? (2M)
3. Describe exception handling in brief.( Description of exception – 2 Marks; syntax – 2 Marks)
4. Write PL/SQL program to print even or odd numbers from given range. (Accept number from
user.) (Correct logic - 2 Marks, correct syntax – 2 Marks)
5. Explain conditional control structure in PL/SQL. (Explanation of each structure - 1 Mark, syntax of each -
1 Mark)
6. What are the advantage of PL/SQL? (Any four advantages – 1 Mark each)
7. Write PL/SQL program to display factorial of any number. (Correct logic – 2 Marks, correct syntax –
2 Marks)
8. Explain implicit and explicit cursors. (Implicit cursor – 2 Marks, Explicit cursor – 2 Marks)
9. Explain parameterized cursor with example. (Explanation - 2 Marks, Any relevant Example - 2 Marks)
10. Give block structure of PL/SQL and explain main components. (Block structure - 2 Marks, Explanation -
2 Marks)