PL SQL Assignments
PL SQL Assignments
Exercise 1
1. Write a program that computes the perimeter and the area of a rectangle. Define your own
values for the
length and width. (Assuming that L and W are the length and width of the rectangle,
Perimeter =
2*(L+W) and Area = L*W. Display the output on the screen using dbms_output.put_line.
Width number:=&width;
Area number;
Parimeter number;
BEGIN Area:=LENGTH*width;
Parimeter:= 2*(LENGTH+width);
END;
2. Write a program that declares an integer variable called num, assigns a value to it, and
computes and
inserts into the tempp table the value of the variable itself, its square, and its cube.
TABLE created.
BEGIN
INSERT INTO tempp
VALUES(num,
num*num,
num*num*num);
END;
Enter value
FOR num: 5 --PL/SQL procedure successfully completed.
SELECT *
FROM tempp;
--ITEM SQUARE CUBE
--5 25 125
3. Convert a temperature in Fahrenheit (F) to its equivalent in Celsius (C) and vice versa. The
required
formulae are:-
C= (F-32)*5/9
F= 9/5*C + 32
Display the output on the screen using dbms_output.put_line. Data has to be input by the
user.
DECLARE F number:=&Fahrenheit;
C number:=&Celsius;
RESULT number;
BEGIN RESULT:=(F-32)*5/9;
RESULT:=9/5*C+32;
END;
/
4. Convert a number of inches into yards, feet, and inches. For example, 124 inches equals 3
yards, 1 foot,
and 4 inches. Display the output on the screen using dbms_output.put_line. Data has to be
input by the
user.
yard int;
foot int;
BEGIN foot:=inch/12;
yard:=foot / 3;
foot:=foot MOD 3;
END;
5. Write a program that enables a user to input an integer. The program should then state
whether the
integer is evenly divisible by 5. (Use decode instead of IF statement where required). Display
the
output on the screen using dbms_output.put_line. Data has to be input by the user.
END IF;
END;
/ BEGIN
SELECT decode(num MOD 5, 0, 'divisible', 'nod
devisible') INTO RESULT
FROM dual;
dbms_output.put_line(RESULT);
END;
6. Your block should read in two real numbers and tell whether the product of the two
numbers is equal to
or greater than 100. Display the output on the screen using dbms_output.put_line. (Use
decode instead
of IF statement where required). Data has to be input by the user.
DECLARE a number:=&firstno;
b number:=&secondno;
RESULT varchar2(50);
BEGIN
SELECT decode(trunc(a*b/100),0,'LESS THEN
100','GREATER THEN OR EQUAL TO 100') INTO RESULT
FROM dual;
dbms_output.put_line(RESULT);
END;
Exercise 2
1. In a PL*SQL block, create a datatype by the name of addr_type. It should contain the
following
components:-
Your block should accept the names and addresses of 4 employees in 4 different variables of
datatype
addr_type. Output the names and addresses of the 4 employees on the screen in the form of
Labels as
shown below:-
*************************************************************
*************************************************************
*************************************************************
* Name:- King ** Name:- Adams *
BEGIN
dbms_output.put_line('*******************************
**********************');
dbms_output.put_line('*******************************
**********************');
dbms_output.put_line('*******************************
**********************');
dbms_output.put_line('*******************************
**********************');
dbms_output.put_line(temp1.street);
END;
Exercise 3
1. Input a number and determine whether it is within a given range (for example, between 1
and 10). The
low and high values of the range may be input by the user rather than be fixed by the
program. Display
the output on the screen using dbms_output.put_line.
ANONYMS-PROGRAM
DECLARE lb number:=&lower_bound;
ub number:=&upper_bound;
DATA number:=#
END IF;
END IF;
END;
/
2. Input three positive integers representing the sides of a triangle, and determine whether
they form a valid
triangle. Hint: In a triangle, the sum of any two sides must always be greater than the third
side. Display
the output on the screen using dbms_output.put_line.
ANONYMS-PROGRAM
DECLARE a number:=&first_side;
b number:=&second_side;
c number:=&third_side;
END IF;
END IF;
END IF;
END;
year should be (divisible by 4 and not divisible by 100) or (divisible by 4 and divisible by
400.) Display the
output on the screen using dbms_output.put_line. The year should be input by the user.
ANONYMS-PROGRAM
result1 int;
result2 int;
result3 int;
BEGIN result1:=mod(YEAR,100);
result2:=mod(YEAR,400);
result3:=mod(YEAR,4);
IF (result3=0)
AND not(result1=0)
OR (result3=0)
AND (result2=0) THEN dbms_output.put_line('leap
year');
END IF;
END;
Calculate the cost of the apple box. Display the output on the screen using
dbms_output.put_line.
ANONYMS-PROGRAM
rate number;
ELSE rate:=7;
END IF;
dbms_output.put_line('TOTAL WEIGHT'||weight||'TOTAL
COST '||(weight*rate));
END;
5. Program should accept the age of the user. Depending upon the following conditions it
should output:-
ANONYMS-PROGRAM
elsif (age>18)
AND (age<21) THEN USER:='major';
ELSE USER:='adult';
END IF;
dbms_output.put_line('user is '||USER);
END;
6. Write a program that asks the user to input two character strings. Your program should
then determine if one
character string exists inside another character string. Display the above on the screen using
dbms_output.put_line.
ANONYMS-PROGRAM
str2 varchar(30):='&second_string';
BEGIN IF instr(str1,str2)>0
OR instr(str2,str1)>0 THEN dbms_output.put_line('one
string contained in another');
ELSE dbms_output.put_line('both are different
string');
END IF;
END;
7. Suppose the grade obtained by a student depends upon his scores and the grading rule is as
follows. :-
Scores Grades
95-100 A
85-94 B
70-84 C
60-69 D
0-59 E
Write a block to accept a student?s marks and accordingly output his grade. Display the
output on the
screen using dbms_output.put_line.
ANONYMS-PROGRAM
grade varchar(1);
END IF;
END;
8. A company manufactures three products:- computer stationery, fixed disks and computers.
The following
codes are used to indicate them:-
Product Code
Computer Stationery 1
Fixed Disks 2
Computers 3
Write a program to accept the order details i.e. product code and order amounts for the
products, calculate
the discount amounts as per this policy and output the net order amount. Display the output
on the screen
using dbms_output.put_line.
ANONYMS-PROGRAM
disc number;
prod varchar2(30);
ELSE disc:=.02;
END IF;
END IF;
END IF;
dbms_output.put_line('_______________________________
___');
dbms_output.put_line('PRODUCT '||prod);
dbms_output.put_line('DISCOUNT IS '||disc);
dbms_output.put_line('_______________________________
___');
END;
Exercise 4
1. Write a program containing a loop that iterates from 1 to 1000 using a variable I, which is
incremented each time around the loop. The program should output the value of I every
hundred
iterations (i.e., the output should be 100, 200, etc). Display the output on the screen using
dbms_output.put_line.
DECLARE i int:=1;
BEGIN LOOP dbms_output.put_line(i);
dbms_output.put_line('____');
i:=i+100;
END LOOP;
END;
2. Write a program that examines all the numbers from 1 to 999, displaying all those for
which the sum of
the cubes of the digits equal the number itself. Display the output on the screen using
dbms_output.put_line.
DECLARE i int:=1;
x int;
y int;
z int;
y:=mod(i,10);
z:=mod(i,10);
IF (x*x*x+y*y*y+z*z*z)=i THEN
dbms_output.put_line(i);
dbms_output.put_line('____');
END IF;
i:=i+1;
END LOOP;
END;
/
3. Write a PL*SQL block that reads in a minimum and maximum value for a radius, along
with an
increment factor, and generates a series of radii by repeatedly adding the increment to the
minimum
until the maximum is reached. For each value of the radius, compute and display the
circumference,
area, and volume of the sphere. (Be sure to include both the maximum and the minimum
values.).
Validate each of the input values to be sure they are positive. If the minimum is typed in
place of the
maximum, swap the values within the program, and continue execution. Display the results
on the
screen using dbms_output.put_line.
j int:=1;
x int;
y int;
z int;
x:=mod(num,10);
num:=trunc(num/10);
y:=mod(num,10);
num:=trunc(num/10);
z:=mod(num,10);
IF (x*x*x+y*y*y+z*z*z)=j THEN
dbms_output.put_line(j);
dbms_output.put_line('____');
END IF;
j:=j+1;
END LOOP;
END;
Allow any positive integer to be typed in. The program should count how many times the
number has to be
doubled before it reaches 1 million. Display the results on the screen using
dbms_output.put_line.
counter int:=1;
num:=2*num;
END LOOP;
END;
4. A palindrome is a word that is spelled the same forward and backward, such as level,
radar, etc. Write a
program to read in a five letter word from the user and determine whether it is a palindrome.
Display
the results on the screen using dbms_output.put_line.
counter int:=length(str);
BEGIN dbms_output.put_line(counter);
counter:=counter-1;
END LOOP;
END IF;
END;
5. Modify the above program to accept a variable length word. This requires determining how
many
characters are read in.
DECLARE str varchar2(50):='&string';
counter int:=length(str);
BEGIN dbms_output.put_line(counter);
counter:=counter-1;
END LOOP;
END IF;
END;
6. Write a program to read in a number and print it out digit by digit, as a series of words. For
example, the
number 523 would be printed as "five two three". Use decode function within a for loop.
Display the
results on the screen using dbms_output.put_line.
i varchar(1);
c int:=length(num);
RESULT varchar(10);
LOOP i:=substr(num,1,1);
num:=substr(num,2);
dbms_output.put_line(RESULT);
c:=c-1;
END LOOP;
END;
Exercise 5
Roll_no Number 4
Total Number 3
Percent Number 5,2
Grade Varchar2 10
Insert into this table the total marks, percentage and grades of the respective students. The
rules for
grades are as follows:-
Percentage Grade
< 50 % FAIL
>= 50 % PASS
For students
Percentage Grade
< 40% FAIL
40 - 49.99% C
50 ? 59.99% B
60 ? 79.99% A
>= 80% HONOURS
TABLES
v_result oracle_result%rowtype;
grade varchar2(10);
CURSOR c1 IS
SELECT *
FROM SCHOOL;
BEGIN
FOR v_student IN c1 LOOP IF v_student.class='Working'
THEN IF v_student.Dev_2000 <50 THEN grade:='FAIL';
ELSE grade:='PASS';
END IF;
ELSE grade:='B';
END IF;
END IF;
END LOOP;
END;
/
Anonyms procedure for oracle result
v_result oracle_result%rowtype;
grade varchar2(10);
CURSOR c1 IS
SELECT *
FROM SCHOOL;
BEGIN
FOR v_student IN c1 LOOP IF v_student.class='Working'
THEN IF v_student.Oracle<50 THEN grade:='FAIL';
ELSE grade:='PASS';
END IF;
END IF;
END IF;
END LOOP;
END;
2. The CUSTOMER table of a state electricity board consists of the following fields:-
There are two types of meters viz. 3- phase or 1-phase coded as ?T? or ?S? respectively.
There are 4 types
of customers viz. Agricultural Industrial, Commercial and Residential with coeds ?A? , ?I?, ?
C? and ?R?
respectively.
Formulae:-
Write a block to calculate the bill for each customer. The program should insert the Meter
no., Units
used, Rate, Amount, Surcharge, Excise duty and Net for each customer into some other
suitable table.
Also, at the end, it should insert the total Amount, Surcharge, Excise and Net into some other
table.
TABLES
CURSOR c1 IS
SELECT *
FROM customer;
rate number(3,2);
units number;
amount number;
surcharge number;
Excise number;
Net number;
BEGIN
DELETE
FROM bill;
units:=v_customer."Current Reading"-
v_customer."Previous Reading";
amount:=rate*units;
surcharge:=surcharge*amount;
Excise:=(amount +Surcharge)*30/100;
END LOOP;
END;
/
OUTPUT
--Compile:
--Call:
EXEC calculatebill
There are ten customers with codes 0 to 9 and five products with codes 0 to 4. The rates of
products are
Rs. 15, 35, 42, 51 and 60 respectively. Write a program to find the total purchase in Rs. of
each
customer and total sale of each product using this table and insert these values in two other
tables.
TABLES
CURSOR c3 IS
SELECT distinct("Product Code ")
FROM corder;
rs number;
TOTAL_PURCHASE number:=0;
TOTAL_SALE number:=0;
BEGIN
DELETE
FROM totalpurchase;
DELETE
FROM totalsale;
FOR i IN c1 LOOP
FOR j IN c2(i."Customer Code ") LOOP
SELECT decode(j."Product Code
",0,15,1,35,2,42,3,51,4,60,0) INTO rs
FROM dual;
TOTAL_PURCHASE:=TOTAL_PURCHASE+rs;
END LOOP;
END LOOP;
rs:=0;
FOR i IN c3 LOOP
FOR j IN c4(i."Product Code ") LOOP
SELECT decode(j."Product Code
",0,15,1,35,2,42,3,51,4,60,0) INTO rs
FROM dual;
TOTAL_SALE:=TOTAL_SALE+rs;
END LOOP;
END LOOP;
END;
OUTPUT
EXEC find_sale_purchase;
SELECT *
FROM totalsale;
SELECT *
FROM totalpurchase;
Category may be ?J?, ?S?, or ?W? for Jr. officers, Sr. officers or Worker category.
Formulae:-
Output the Employee Number and the Gross for each employee in a separate table.
TABLES
PROCEDURE
emp_record employee%rowtype;
da number(20,2);
hra number(20,2);
gross number(20,2);
BEGIN
DELETE
FROM employee_gross;
FOR emp_record IN c1 LOOP da:=emp_record."Basic
Salary "*35/100;
IF emp_record.Category='j'
AND hra>250 THEN hra:=250;
elsif emp_record.Category='s'
AND hra>1000 THEN hra:=1000;
elsif emp_record.Category='w'
AND hra>30000 THEN hra:=30000;
ELSE hra:=0;
END IF;
END LOOP;
END;
/
Output
EXEC gross;
Exercise 6
1. The median of an array of numbers is the element m of the array such that half the
remaining numbers in the array are greater than
or equal to m and half are less than or equal to m, if the number of elements in the array is
odd. If the number of elements is even,
the median is the average of the two elements m1 and m2 such that half the remaining
elements are greater than or equal to m1
and m2, and half the elements are less than or equal to m1 and m2. Write a PL*SQL block
that allows the user to enter 10
elements in a number array and outputs the median of the numbers in the array. Write another
PL*SQL block that allows the user
to enter 11 elements in a number array and outputs the median of the numbers in the array.
Display the outputs on the screen
using dbms_output.put_line.
2. The mode of an array of numbers is the number m in the array that is repeated most
frequently. If more than 1 number is repeated
with equal maximum frequencies, there is no mode. Write a PL*SQL block that allows the
user to enter 10 elements in a number
array and outputs the mode or indication that the mode does not exist. Display the above
output on the screen using
dbms_output.put_line.
Read a group of 10 temperature readings into two number arrays. A reading consists of two
numbers:- an integer between ?90 and
90, representing the latitude at which the reading was taken, and the observed temperature at
that latitude. Print a table (display
on screen in tabular format) consisting of each latitude and the average temperature at that
latitude. If there are no 2 sets of
readings at a particular latitude, print ?NO DATA? instead of an average. Then print the
average temperature in the northern and
southern hemispheres (the northern consists of latitudes 1 through 90 and the southern
consists of latitudes ?1 through ?90). (This
average temperature should be computed as the average of the averages, not the average of
the original readings). Also determine
which hemisphere is warmer. In making the determination, take the average temperatures in
all latitudes of each hemisphere for
which there are data for both that latitude and the corresponding latitude in the other
hemisphere. (For example, if there is data
for latitude 57 but not for latitude ?57, then the average temperature for latitude 57 should be
ignored in determining which
hemisphere is warmer). Display the above output on the screen using dbms_output.put_line.
4. Write a PL*SQL block to accept a number from the user. With the help of PL*SQL arrays,
write a program for Number to word
conversion up to 99 crores. The program should cater to Rs. and paise also.
123451250.75
Rs. Twelve crores, Thirty Four lakhs, Fifty One thousand, Two hundred and Fifty
9728
The output of your program should be:-
Rs. Nine thousand, Seven hundred and Twenty Eight only.
5. Write a PL*SQL block to accept a character string from the user. The user should enter a
number spelt out. With the help of
PL*SQL arrays, write a program for Word to number conversion up to 99 crores. The
program should cater to Rs. and paise also.
123451250.75
If the user enters:-
Exercise 7
1. Write a PL*SQL block that prompts the user to enter the salary of an employee. Your
program should
display the name of the employee (from the EMP table) who?s getting that salary. If more
than 1
employee is receiving that salary, or if no employees exist getting that salary, your program
should
display appropriate messages. Use too_many_rows and no_data_found exceptions to achieve
this.
Display the results on the screen using dbms_output.put_line.
TABLE
EXCEPTION CODE
sal NUMBER:=&salary;
BEGIN
SELECT basicsalary INTO mysal
FROM emp
WHERE basicsalary =sal;
exception WHEN too_many_rows THEN
dbms_output.put_line('too many data found in result
can''t handel ');
END;
2. Write a PL*SQL block to check if any employee from EMP table is receiving a salary
greater than
9999.99. Make the use of value_error exception to achieve this. Display the results on the
screen using
dbms_output.put_line.
EXCEPTION CODE
sal NUMBER:=&salary;
BEGIN
INSERT INTO emp
VALUES('1003',
'kumar',
'sr.off',
's',
sal,
sysdate);
END;
3. Create a user-defined exception by the name of exp_check. Select the ename and hiredate
of all
employees into a cursor. Your program should calculate the experience of all the employees
in years,
and insert the ename and experience of each employee into tempp table. If any employee has
experience
less than 2 years, the program should be aborted with a suitable message. Raise the user-
defined
exception exp_check to achieve this. Display the results on the screen using
dbms_output.put_line.
EXCEPTION CODE
CURSOR c1 IS
SELECT empname,
joined
FROM emp;
BEGIN
FOR i IN c1 LOOP
if(trunc(months_between(sysdate,i.joined)/12)<2) THEN
RAISE exp_check;
ELSE
INSERT INTO tempp2
VALUES(i.empname,
trunc(months_between(sysdate,i.joined)/12));
END IF;
END LOOP;
END;
4. Write a PL*SQL function to take three parameters, the sides of a triangle. The sides of the
triangle
should be accepted from the user. The function should return a Boolean value:- true if the
triangle is
valid, false otherwise. A triangle is valid if the length of each side is less than the sum of the
lengths of
the other two sides. Check if the dimensions entered by the user can form a valid triangle.
Display the
results on the screen using dbms_output.put_line.
EXCEPTION CODE
END IF;
RETURN FALSE;
END;
CALLING-CODE
DECLARE a number:=&side1;
b number:=&side2;
c number:=&side3;
x boolean;
BEGIN x:=triangle(a,b,c);
END /
Enter value
FOR side1: 2 OLD 2: a number:=&side1;
NEW 2: a number:=2;
Enter value
FOR side2: 3 OLD 3: b number:=&side2;
NEW 3: b number:=3;
Enter value
FOR side3: 5 OLD 4: c number:=&side3;
NEW 4: c number:=5;
5. Write a function that generates a random number between 1 and 10. Use any logic of your
choice to
achieve this. Display the results on the screen using dbms_output.put_line.
RANDOM METHOD
DECLARE x number;
BEGIN
SELECT trunc(dbms_random.value(1,10)) INTO x
FROM dual;
dbms_output.put_line(x);
END;
/
CREATING-RANDOM -NO
END IF;
dbms_output.put_line(seed);
END;
CALLING-CODE
n number:=&number;
BEGIN seed :=
EXP(TO_NUMBER(TO_CHAR(SYSDATE,'ss'))/59)-1;
n:=trunc(n/10)-1;
dbms_output.put_line(trunc(seed,n)*power(10,n));
END;
6. Design a structure to store length in yards, feet, and inches (for example, 7 yards, 2 feet, 3
inches). Your
program should accept 2 length measurements from the user. Write a PL*SQL procedure to
find the
difference between two measurements as represented by these structures. Display the results
on the
screen using dbms_output.put_line.
ANONYMS-PROGRAM
DECLARE y number:=&yard;
f number:=&feet;
i number:=&inch;
y2 number:=&yard2;
f2 number:=&feet2;
i2 number:=&inch2;
BEGIN i:=y*3*12+f*12+i;
i2:=y2*3*12+f2*12+i2;
i:=i-i2;
y:=trunc(i/(3*12));
i:=mod(i,(3*12));
f:=trunc(i/(12));
i:=mod(i,12);
END;
7. Create a function that accepts a string of n characters and exchanges the first character with
the last, the
second with the next ? to ? last, and so forth until n exchanges have been made. What will the
final
string look like? Write the function to verify your conclusion. Display the results on the
screen using
dbms_output.put_line.
FUNCTION
BEGIN
FOR j IN 1..len LOOP st:=st||substr(st,len-j,1);
END LOOP;
st:=substr(st,len);
len:=length(st);
st:=substr(st,1,len-1);
RETURN TRUE;
END;
CALLING-CODE
DECLARE k boolean;
BEGIN k:=revstr(val);
END;
Exercise 8
1. Write a stored procedure by the name of Comp_intr to calculate the amount of interest on a
bank
account that compounds interest yearly. The formula is:-
I = p (1+ r/100) y ? p
where:-
Your stored procedure should accept the values of p, r and y as parameters and insert the
Interest and
Total amount into tempp table.
PROCEDURE
BEGIN I:=P*POWER(1+R,Y)-P;
DBMS_OUTPUT.PUT_LINE(I);
END;
CALLING -PROCEDURE
BEGIN comp_intr(1600,9,4);
END;
OR EXEC comp_intr(1600,9,4);
2. Create a stored function by the name of Age_calc. Your stored function should accept the
date of birth
of a person as a parameter. The stored function should calculate the age of the person in
years, months
and days e.g. 35 years, 3 months, 17 days. The stored function should return the age in years
directly
(with the help of Return statement). The months and days are to be returned indirectly in the
form of
OUT parameters. Write a PL*SQL block to accept the date of birth of an employee from the
user, call
the stored function, and display the age of the employee on the screen. Display the above
results on the
screen using dbms_output.put_line.
FUNCTION
BEGIN d:=sysdate-dat;
y:=d/365;
y:=trunc(y);
m:=(d-y*365)/30;
M:=trunc(m);
d:=trunc(d-y*365-m*30);
RETURN y;
END;
/
CALLING -PROCEDURE
DECLARE D varchar2(20):='r';
P1 NUMBER:=&day_of_birts;
P2 NUMBER:=&month_of_birth;
P3 NUMBER:=&year_of_birth;
BEGIN D:=to_char(p1)||'-'||to_char(p2)||'-'||
to_char(p3);
P1:=AGE_CALC(to_date(d,'dd-mm-yyyy'),P2,P3);
END;
3. Create a package by the name of Payroll_calc. The package should contain separate
procedures for DA,
HRA, Gross, Tax and Net calculation.
Formulae:-
If Gross exceeds 4000, Tax is to be deducted at 5% of the amount exceeding 4000. If Gross
exceeds
5000, Tax is to be deducted at 5% of the amount exceeding 4000 and an additional of 2% of
the amount
exceeding 5000.
Write a PL*SQL block that calls the procedures from the above package. The PL*SQL block
should
print the Pay slips for all the employees. The format of the Pay slip should be as follows:-
******************************************************************
Sal:- xxx DA:- xxx HRA:- xxx Gross:- xxx Tax:- xxx Net:- xxx
******************************************************************
Exercise 9
Ord_dtl
Ord_no Prod_cd Qty
1 P1 100
1 P2 200
Prod_mst
Prod_cd Prod_name Qty_in_stock Booked_qty
P1 Floppies 10000 1000
P2 Printers 5000 600
P3 Modems 3000 200
1. Write a Before Insert trigger on Ord_dtl. Anytime a row is inserted in Ord_dtl, the
Booked_qty in Prod_mst
should be increased accordingly.
TABLES
TRIGGER
OUTPUT
SQL>
INSERT INTO Ord_dtl
VALUES(1,
'P1',
50);
SQL>
SELECT *
FROM ord_dtl;
--ORD_NO PR QTY
--1 P1 100
--1 P2 200
--1 P1 50
SQL>
SELECT *
FROM prod_mst;
2. Write a Before Delete trigger on Ord_dtl. Anytime a row is deleted from Ord_dtl, the
Booked_qty in
Prod_mst should be decreased accordingly.
TRIGGER
OUTPUT
SQL>
DELETE
FROM ord_dtl
WHERE qty=50;
3. Write a Before Update of Prod_cd, Qty trigger on Ord_dtl. Anytime the Prod_cd or Qty is
updated, the
Booked_qty in Prod_mst should be increased/decreased accordingly.
TRIGGER
prod_cd=:new.prod_cd
WHERE prod_cd=:old.prod_cd; END;
FIRST UPDATE
SQL>
SELECT *
FROM ord_dtl;
--ORD_NO PR QTY
--1 P1 100
--1 P2 200
SQL>
SELECT *
FROM prod_mst;
--ORD_NO PR QTY
--1 P1 300
--1 P2 200
SQL>
SELECT *
FROM prod_mst;
SECOND UPDATE
SQL>
UPDATE ord_dtl
SET prod_cd='P3',
qty=300
WHERE prod_cd='P1';
--1 row updated.
SQL>
SELECT *
FROM prod_mst;
4. Write a Before Update of Status trigger on Ord_mst. If the Status is updated from P
(Pending) to D
(Delivered), the Booked_qty and Qty_in_stock from Prod_mst should be decreased
accordingly. If the
Status is updated from P (Pending) to C (Cancelled), the details of the order should be deleted
from Ord_dtl
and corresponding Booked_qty from Prod_mst should be decreased accordingly. (The Before
delete trigger
on Ord_dtl would automatically decrease the Booked_qty from Prod_mst).
TRIGGER
CURSOR c1 IS
SELECT *
FROM Ord_dtl
WHERE Ord_no=:old.Ord_no; BEGIN IF :NEW.STATUS='c'
OR :NEW.STATUS='C' THEN
DELETE
FROM ORD_DTL WHERE ORD_NO=:OLD.ORD_NO; END IF; IF
(:NEW.STATUS='d')
OR (:NEW.STATUS='D') THEN
FOR i IN c1 LOOP
UPDATE PROD_MST
SET QTY_IN_STOCK=(QTY_IN_STOCK- i.QTY),
booked_QTY=(booked_QTY-i.QTY) WHERE
Prod_cd=i.Prod_cd; dbms_output.put_line('updated as
cancel in prod_mst'); END LOOP; END IF; END; /
OUTPUT
OPERATION 1: SQL>
UPDATE ord_mst
SET status='c';