DMS Unit IV
DMS Unit IV
UNIT-4 ( 18Marks)
MSBTE Question bank
68
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
Exception Handling: Exception is nothing but an error. 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.
Exception handling is nothing but a code block in memory that will
attempt to resolve
current error condition.
Syntax:
DECLARE ;
Declaration section
…executable statement;
EXCEPTION
WHEN ex_name1 THEN ;
Error handling statements/user defined action to be carried out;
END;
Types of Exception:
1) Predefined Exception/system defined exception/named
exception: Are always
automatically raised whenever related error occurs. The most
common errors that can
occur during the execution of PL/SQL. Not declared explicitly i.e.
cursor already open,
invalid cursor, no data found, zero divide and too many rows etc.
Programs are handled
by system defined Exceptions.
2) User defined exception: It must be declare by the user in the
declaration part of the
block where the exception is used. It is raised explicitly in
sequence of statements using:
Raise_application_error(Exception_Number, Error_Message);
3 Explain function PL\SQL with example W-18 4
Function: Description:
Function is a logically grouped set of SQL and Pl/SQL statements 2
that perform a marks,
specific task. A function is same as a procedure except that it Example : 2
returns a value. A marks
function is created using the CREATE FUNCTION statement.
Syntax:
CREATE [OR REPLACE] FUNCTION function_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
RETURN return_datatype
{IS | AS}
BEGIN
< function_body >
END [function_name];
Where,
-name specifies the name of the function.
69
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
function.
the parameters. IN
represents the value that will be passed from outside and OUT
represents the
parameter that will be used to return a value outside of the
procedure.
tain a return statement.
a standalone
function.
Example:
CREATE OR REPLACE FUNCTION Success_cnt
RETURN number
IS cnt number(7) := 0;
BEGIN
SELECT count(*) into cnt
FROM candidate where result='Pass';
RETURN cnt;
END;
/
4 Draw the block structure of PL/SQL . List advantages of PL/SQL W-18 4
70
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
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 Write step by step syntax to create, open and close cursor in W-18, 4
PL/SQL. S-23
A cursor holds the rows (one or more) returned by a SQL 2 marks,
statement. Opening: 1
Declaring: This term is used to declare a cursor so that memory mark,
initialization will take place.A cursor is declared by defining the Closing
SQL statement that returns a result set. cursor: 1
Example: mark
Declare CURSOR Winter_18 IS SELECT roll_no, std_name,
percentage FROM
student;
Opening: A Cursor is opened and populates data by executing the
SQL statement defined by the cursor.
Example:
Open Winter_18;
Closing a Cursor: This forces cursor for releasing the allocated
memory assigned/ occupied by cursor.
Example:
CLOSE Winter_18;
6 Write a PL/SQL program which accept the customer ID from the W-18 6
user if user enters an invalid ID then the exception invalid_id is
raised using exception handling.
71
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
DECLARE
c_id numeric(10);
invalid_id_Exception Exception;
BEGIN
c_id:=&c_id;
if(c_id<0) then
raise invalid_id_Exception;
end if;
EXCEPTION
WHEN invalid_id_Exception THEN
dbms_output.put_line('Invalid customer id');
END;
1 State any four PL/SQL data types. S-19 2
1. NUMBER or NUMBER(P,S)
2. PLS_INTEGER
3. CHAR
4. RAW
5. ROWID
6. VARCHAR2
7. DATE
2 Explain PL/SQL block structure with the help of diagram S-19, 4
S-22
Declaration section
A block begins with declarative section where variables, cursors
are declared. It is an Optional block.
Execution section
Executable SQL or PL/SQL Statements are needed to write here
for the execution. It is mandatory block.
Exception section
It is used to handles the exceptions. It is an Optional block.
End statement
It is used to indicate termination of PL/SQL block. It is mandatory
3 Explain cursor with example S-19 4
72
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
A cursor is a temporary work area created in system memory when
a SQL statement is executed. A cursor is a set of rows together with
a pointer that identifies a current row. It is a database object to
retrieve data from a result set one row at a time. It is useful when we
want to manipulate the record of a table in a singleton method, in
other wordsone row at a time. In other words, 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.
Each cursor contains the followings 4 steps,
1. Declare Cursor: In this part we declare variables and return a set
of values.
2. Open: This is the entering part of the cursor.
3. Fetch: Used to retrieve the data row by row from a cursor.
4. Close: This is an exit part of the cursor and used to close a
cursor.
5. Eg:
Declare
enumemp.eno%type;
enemp.ename%type;
Cursor cur is select eno, ename from emp where jobname = “mgr”;
Begin
Open cur;
Loop Fetch cur into enum,en;
Exit when cur%NOTFOUND;
Dbms_output.put_line(„emp num ‟||enum||‟ emp name „||en);
End loop;
Close cur;
End; /
The example shows fetching multiple records using cursor. A
cursor is a temporary work area created in system memory when a
SQL
statement is executed. A cursor is a set of rows together with a
pointer that identifies a current row.
In the example, the cursor is defined to hold the rows as defined by
the select query. Once the cursor is defined, the next step is to open
the cursor. When the cursor is opened, it is ready to retrieve the
rows. This is done using the fetch statement. Since there are many
rows, a loop is used to display the values of all the rows.
Once the rows are fetched, the
cursor should be closed.
4 State the use of database trigger and also list types of trigger S-19 4
Use of trigger
Trigger: A trigger is a stored procedure in database which
automatically
invokes whenever a special event in the database occurs. A trigger
can be invoked when a row is inserted into a specified table or
when certain table columns are being updated.
Triggers are written to be executed in response to any of the
following
73
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
events − A database manipulation (DML) statement (DELETE,
INSERT, or
UPDATE)
Database definition (DDL) statements (CREATE, ALTER, or
DROP).
A database operation (SERVERERROR, LOGON, LOGOFF,
STARTUP, or SHUTDOWN).
Triggers can be defined on the table, view, schema, or database
with
which the event is associated.
Triggers can be written for the following purposes –
referential integrity
Types of trigger
s
5 Write a PL/SQL program to calculate factorial of a given number S-19 6
DECLARE
num number:=#
fact number:=1;
BEGIN
while num!=0
loop
fact:=fact*num;
num:=num-1
end loop;
dbms_output.put_line(‘Factorial =’||fact);
END;
/
(OR)
DECLARE
num number:=#
fact number:=1;
i number;
BEGIN
for i in 1..num loop
fact:=fact*i;
end loop;
74
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
database details from the user.
times
2 Explain implicit and explicit cursors W-19 4
A cursor is a temporary work area created in system memory when
an SQL statement is executed. A cursor is a set of rows together
with a pointer that identifies a current row. It is a database object
to retrievedata from result set on row at a time.
Implicit cursor: these types of cursors are generated and used by
the system during the manipulation of a DML query. An implicit
cursor is also generated by the system when a single row is selected
by a SELECT command. Programmers cannot control the implicit
cursors.
Explicit cursor: this type of cursor is created by the user when the
select command returns more than one row, and only one row is to
be
processed at a time. An explicit cursor can move from one row to
another in a result set. An explicit cursor uses a pointer that holds
the
record of a row.
To create an explicit cursor the following steps are used.
1. Declare cursor: this is done in the declaration section of PL/SQL
program.
2. Open: this step is done before the cursor is used to fetch the
records.
3. Fetch: used to retrieve data row by row from the cursor.
4. Close: once the processing of the data is done, the cursor can be
closed
3 Explain Exception handling with it’s types W-19 4
An exception is an error condition during a program execution.
PL/SQL supports programmers to catch such conditions
using EXCEPTION block in the program and an appropriate action
is taken against the error condition.
There are two types of exceptions −
-defined exceptions/Predefined exceptions/Built-in
Exceptions
2) User-defined exception
1) Predefined exceptions- PL/SQL provides predefined Exception,
which are executed when any database rule is violated by a
program.
Example: NO_DATA_FOUND, ZERO_DIVIDE.
Syntax for Predefined Exception Handling:
The general syntax for exception handling is as follows.
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
75
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
<exception handling goes here >
WHEN exception1 THEN
exception1-handling-statements
WHEN exception2 THEN
exception2-handling-statements
WHEN exception3 THEN
exception3-handling-statements
........
WHEN others THEN
exception3-handling-statements
END;
2) User defined Exceptions:
PL/SQL allow us to define our own exception according to the
need
of our program. A user defined exception must be declared and
then
raised explicitly.
Syntax for User defined Exception:
DECLARE
exception_name EXCEPTION;
BEGIN
IF condition THEN
RAISE exception_name;
END IF;
EXCEPTION
WHEN exception_name THEN
statement;
END;
4 Define database trigger. How to create and delete trigger ? W-19 4
Triggers are stored programs, which are automatically executed or
fired when some events occur. Triggers are, in fact, written to be
executed in response to any of the following events −
INSERT, or UPDATE)
DROP).
Triggers can be defined on the table, view, schema, or database
with
which the event is associated.
Creating Triggers
The syntax for creating a trigger is −
CREATE [OR REPLACE ] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
WHEN (condition)
DECLARE
76
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
Declaration-statements
BEGIN
Executable-statements
EXCEPTION
Exception-handling-statements
END;
To delete a trigger:
Syntax:
DROP TRIGGER trigger_name.
5 Explain any one control structure in PL/SQL with example W-19 4
PL/SQL has three categories of control statements: conditional
selection statements, loop statements and sequential control
statements.
PL/SQL categories of control statements are:
programming.
The sequential control statements are GOTO, which goes to a
specified statement, and NULL, which does nothing.
1)Conditional Control: IF and CASE Statements:
The IF statement lets us execute a sequence of statements
conditionally. That is, whether the sequence is executed or not
depends on the value of a condition. There are three forms
of IF statements: IF-THEN, IF-THEN-ELSE, and IF-THEN-
ELSIF.
The CASE statement is a compact way to evaluate a single
condition
and choose between many alternative actions.
IF-THEN Statement:The simplest form of IF statement associates
a
condition with a sequence of statements enclosed by the
keywords THEN and END IF (not ENDIF), as follows:
IF condition THEN
sequence_of_statements
END IF;
IF-THEN-ELSE Statement: The second form of IF statement adds
the keyword ELSE followed by an alternative sequence of
statements,
as follows:
77
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
IF condition THEN
sequence_of_statements1
ELSE
sequence_of_statements2
END IF;
IF-THEN-ELSIF Statement: The third form of IF statement uses
the keyword ELSIF (not ELSEIF) to introduce additional
conditions,
as follows:
IF condition1 THEN
sequence_of_statements1
ELSIF condition2 THEN
sequence_of_statements2
ELSE
sequence_of_statements3
END IF;
CASE Statement: Like the IF statement, the CASE statement
selects
one sequence of statements to execute.
IF grade = 'A' THEN
dbms_output.put_line('Excellent');
ELSIF grade = 'B' THEN
dbms_output.put_line('Very Good');
ELSIF grade = 'C' THEN
dbms_output.put_line('Good');
ELSIF grade = 'D' THEN
dbms_output. put_line('Fair');
ELSIF grade = 'F' THEN
dbms_output.put_line('Poor');
ELSE
dbms_output.put_line('No such grade');
END IF;
END CASE;
2) Iterative Control: LOOP and EXIT Statements:
LOOP statements let us execute a sequence of statements multiple
times. There are three forms of LOOP statements: LOOP,
WHILELOOP, and FOR-LOOP.
LOOP: The simplest form of LOOP statement is the basic (or
infinite) loop, which encloses a sequence of statements between
the
keywords LOOP and END LOOP, as follows:
LOOP
sequence_of_statements
END LOOP;
WHILE-LOOP:The WHILE-LOOP statement associates a
condition with a sequence of statements enclosed by the
keywords LOOP and END LOOP, as follows:
WHILE condition LOOPsequence_of_statements
END LOOP;
Before each iteration of the loop, the condition is evaluated. If the
78
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
condition is true, the sequence of statements is executed, then
control resumes at the top of the loop. If the condition is false or
null, the
loop is bypassed and control passes to the next statement.
FOR-LOOP:Whereas the number of iterations through
a WHILE loop is unknown until the loop completes, the number
of iterations through a FOR loop is known before the loop is
entered. FOR loops iterate over a specified range of integers. The
range is part of an iteration scheme, which is enclosed by the
keywords FOR and LOOP. A double dot (..) serves as the range
operator. The syntax follows:
FOR counter IN [REVERSE] lower_bound..higher_bound LOOP
sequence_of_statements
END LOOP.
6 Write a PL/SQL program to print n even numbers using For Loop W-19 6
declare Correct
num number; logic 3M
n number:=&n; Correct
begin syntax
for num in 1..n loop 3M
if(mod(num,2)=0) then
dbms_output.put_line(‘Even no are :’||num);
end if;
end loop;
end;
79
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
going to be shown. Else in any other cases there is no opportunity
for outputs.
Syntax
IF <condition> then
Statement 1
Statement 2
.
.
.
.
Statement N
END IF;
Example Queries
SQL> DECLARE
2 N1 INTEGER;
3 BEGIN
4 N1:=10;
5 IF N1 > 10 THEN
6 dbms_output.put_line('The Value is' || N1);
7 END IF;
8 END;
9/
OUTPUT:
INSERT, or UPDATE)
DROP).
Triggers can be defined on the table, view, schema, or database
with
which the event is associated.
Creating Triggers
The syntax for creating a trigger is −
CREATE [OR REPLACE ] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
WHEN (condition)
DECLARE
Declaration-statements
80
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
BEGIN
Executable-statements
EXCEPTION
Exception-handling-statements
END;
To delete a trigger:
Syntax:
DROP TRIGGER trigger_name.
3 Write a PL/SQL program, which accept the number from user. If S-22 6
user enters an odd number then exception invalid number is raised
using user defined exception handling.
DECLARE
v_input VARCHAR2(10) := 'abc';
v_number NUMBER;
BEGIN
BEGIN
v_number := TO_NUMBER(v_input);
DBMS_OUTPUT.PUT_LINE('Conversion successful.
Number: ' || v_number);
EXCEPTION
WHEN INVALID_NUMBER THEN
DBMS_OUTPUT.PUT_LINE('Error: Invalid number');
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
82
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
4. on [table_name]: This specifies the name of the table associated
with the trigger.
5. [for each row]: This specifies a row-level trigger, i.e., the trigger
will be executed
for each row being affected.
6. [trigger_body]: This provides the operation to be performed as
trigger is fired
Example:
Given Student Report Database, in which student marks
assessment is recorded. In
such schema, create a trigger so that the total and percentage of
specified marks is
automatically inserted whenever a record is insert.
Here, as trigger will invoke before record is inserted so, BEFORE
Tag can be used.
create trigger stud_marks
before INSERT
on
Student
for each row
set Student.total = Student.subj1 + Student.subj2 + Student.subj3,
Student.per
= Student.total * 60 / 100;
4 Write a trigger which invokes on deletion of record on emp table W-22 4
Delete Trigger: 4M
create or replace trigger trg1 before delete on emp_details Or any other
declare correct logic
begin for delete
raise_application_error(-20000,'cannot delete the record'); trigger
end
5 Write a PL/SQL code to check whether specified employee is W-22 6
present in Emp table or not. Accept empno from user. If employee
does not exist display message using exception handling.
PL/SQL Program: 2M
declare =declaration
no emp.empno%type; 2M=select
begin statement
no:=&no; 2M=proper
select empno into no from emp where empno=no; exception
dbms_output.put_line('Empno is present: '||no); message
exception
when no_data_found then
dbms_output.put_line('Empno not present');
end;
83
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
2 State syntax of while loop command S-23 2
Syntax :
WHILE <condition>
LOOP statements;
END LOOP;
3 Explain subquery concept with example S-23 2
Subquery : A subquery is a query which is embedded within Explanati
another query. The outer query is called as main query and inner on 1M
query is called as subquery Example
For Example: 1M
Select * from customer where age = ( Select min(age) from
customer );
The above query will select all rows from customer table with
minimum age.
4 Explain trigger with suitable example S-23 4
Trigger: Correct
Triggers are stored programs, which are automatically executed or explanati
fired when some events occur. Triggers are, in fact, written to be on 2M
executed in response to any Any
of the following events - relevant
example
or UPDATE) 2M
DROP).
Triggers can be defined on the table, view, schema, or database
with which the
event is associated. a
For Example:
create trigger trigger_update
on emp
after update
as begin Select * from employee;
end;
84
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
The above trigger will invoke after update of record in emp table.
5 Write a pl sql program to print odd number from 1to 10 S-23 4
begin 2M for
dbms_output.put_line(„Oddnos from 1 to 10 are :‟); Correct
fornum in 1..10 Logic
loop 2M for
if(mod(num,2)!=0) then correct
dbms_output.put_line(num); syntax
end if;
end loop;
end;
6 Explain for loop syntax in PLSQL with suitable example of S-23 6
printing 10 to 1 reverse number
For Loop :
execute a
sequence of statements once for each integer in the range.
the parameters.
IN
represents the value that will be passed from outside and OUT
represents the
parameter that will be used to return a value outside of the
procedure.
86
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
EXCEPTION
<exception handling goes here >
WHEN exception1 THEN
exception1-handling-statements
WHEN exception2 THEN
exception2-handling-statements
WHEN exception3 THEN
exception3-handling-statements
........
WHEN others THEN
exception3-handling-statements
END;
2) User defined Exceptions:
PL/SQL allow us to define our own exception according to the
need of our program. A
user defined exception must be declared and then raised explicitly.
Syntax for User defined Exception:
DECLARE
exception_name EXCEPTION;
BEGIN
IF condition THEN
RAISE exception_name;
END IF;
EXCEPTION
WHEN exception_name THEN
statement;
END;
Example:
DECLARE
A number:=20;
B number:=0;
C number;
BEGIN
dbms_output.put_line(‘First Num : ’||A);
dbms_output.put_line(‘Second Num : ’||B);
C:= A / B;
--Raise built in Exception if B is 0
dbms_output.put_line(‘ Result ’ || C);-- and then Result will not be
displayed
EXCEPTION
WHEN ZERO_DIVIDE THEN
dbms_output.put_line(‘ Trying to Divide by zero :: Error ’);
END;
2 Explain procedures in PL\SQL with example W-23 4
(any relevant example should be considered)
The PL/SQL stored procedure or simply a procedure is a PL/SQL
block which performs one or more specific tasks. It is just like
procedures in other programming languages.
The procedure contains a header and a body. Header: The header
contains the name of the procedure and the parameters or
87
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
variables passed to the procedure.Body: The body contains a
declaration section, execution section and exception
section similar to a general PL/SQL block.When you want to
create a procedure or function, you have to define parameters.
There is
three ways to pass parameters in procedure:
1. IN parameters: The IN parameter can be referenced by the
procedure or function
The value of the parameter cannot be overwritten by the
procedure or the function.
2. OUT parameters: The OUT parameter cannot be
referenced by the procedure or
function, but the value of the parameter can be overwritten
by the procedure or
function.
3. INOUT parameters: The INOUT parameter can be
referenced by the procedure
or function and the value of the parameter can be
overwritten by the procedure or
function.
PL/SQL Create Procedure
Syntax for creating procedure:
CREATE [OR REPLACE] PROCEDURE
procedure_name
[ (parameter [,parameter]) ]
IS
[declaration_section]
BEGIN
executable_section
[EXCEPTION
exception_section]
END [procedure_name];
Example:
The following example creates a simple procedure that
displays the string 'Hello World!'
on the screen when executed
CREATE OR REPLACE PROCEDURE greetings
AS
BEGIN
dbms_output.put_line('Hello World!');
END;
/
When above code is executed using SQL prompt, it will
produce the following result:
Procedure created.
PL/SQL program to call procedure
A standalone procedure can be called in two ways:
· Using the EXECUTE keyword
· Calling the name of the procedure from a PL/SQL block
88
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
The above procedure named 'greetings' can be called with
the EXECUTE keyword as:
EXECUTE greetings;
The procedure can also be called from another PL/SQL
block:
BEGIN
greetings;
END;
/
Syntax for drop procedure
DROP PROCEDURE procedure_name;
Example of drop procedure
BEGIN
DROP PROCEDURE greetings;
END;
/
3 Describe sequence in PL\SQL with example W-23 4
(Any relevant example should be considered)
A sequence is used to generate numbers in sequence. You can use
sequences to insert unique values in Primary Key and Unique Key
columns of tables. To create a sequence,
give the CREATE SEQUENCE statement.
Syntax for creating sequence:
CREATE SEQUENCE sequence_name
START WITH initial_value
INCREMENT BY increment_value
MINVALUE minimum value
MAXVALUE maximum value
CYCLE|NOCYCLE ;
.where as:sequence_name: Name of the sequence.
initial_value: starting value from where the sequence starts.
Initial_value should be greater than or equal to minimum value and
less than equal to maximum value.
increment_value: Value by which sequence will increment itself.
Increment_value can be positive or negative.
minimum_value: Minimum value of the sequence.
maximum_value: Maximum value of the sequence.
cycle: When sequence reaches its set_limit it starts from beginning.
nocycle: An exception will be thrown if sequence exceeds its
max_value.
Example
CREATE SEQUENCE sequence_1
start with 1
increment by 1
minvalue 0
maxvalue 100
cycle;
Alter sequence:
Syntax:
alter sequence <sequence_name> maxvalue <number>;
89
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
Alter sequence can change the maxvalue in the sequence created.
Example:
ALTER SEQUENCE employee_id_seq
INCREMENT BY 1
START WITH 10000
MINVALUE 1
MAXVALUE 99999
CACHE 10;
Dropping sequence:
Syntax:
drop sequence <sequence_name>;
To drop the sequence the DROP command is used.
Example:
DROP SEQUENCE employee_id_seq;
4 Compare database trigger and procedure(write four diierences and W-23 4
explain theuse of database trigger
5 Write a PL\SQL program to print sum of n odd numbers for using W-23 6
for loop
(Any other correct logic should be considered)
Declare
i number :=1;
sum number := 0;
n number;
begin
n := &n;
for i in 1..n
loop
if mod(i,2) = 1
then
sum := sum + i;
end if;
end loop;
dbms_output.put_line(‘sum= ’||sum);
90
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
end;
OR
Declare
i number :=1;
sum number := 0;
n number;
begin
n := &n;
for i in 1..n
loop
sum := sum + i;
i := i + 2;
end loop;
dbms_output.put_line(‘sum= ’||sum);
end;
1 Write PL/SQL code to print largest number from three numbers S-24 4
(accept 3 numbers from user)
declare
a number:=10;
b number:=12;
c number:=5;
begin
dbms_output.put_line('a='||a||' b='||b||' c='||c);
if a>b AND a>c
then
dbms_output.put_line('a is greatest');
else
if b>a AND b>c
then
dbms_output.put_line('b is greatest');
else
dbms_output.put_line('c is greatest');
end if;
end if;
end;
/
Output
91
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
3 Write and explain syntax to create ,open and close cursor in S-24 4
PL/SQL
92
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
5 Explain use of database trigger .list types of trigger writ command S-24 6
to create and delete trigger
93
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE
94
RSCOEP COMPUTER ENGINEERING DEPARTMENT MRS.S.S.GHULE