Professional Documents
Culture Documents
No: 12
Program to implement PL/SQL to show the uses of explicit cursor
Ex.No: 13
Write a PL/SQL procedure to calculate electricity charges of each user. The rates are
depending on whether the user is residential(R), Commercial(C), or Industrial (I).The rates are
computed as follows:
Name varchar2(20)
Type Char
Unit_Consumed number(7,2)
ALGORITHM
1. Start.
2. Define the variable cid to get Cons_no and charge to calculate the amount.
4. Calculate Consumed=p_read-v_read.
6. Charge = 60 + (.75*consumed)
8. Charge:=100+(2*consumed);
10. Charge:=500+(3*consumed);
12. Stop.
QUERY
SQL> Create table Customer (Cons_no number(6) primary key not null, Name varchar2(20),
type char,unit_consumed number(7,2));
Table created.
1 row created.
1 row created.
PL/SQL BLOCK
declare
cid varchar2(10):='&cid';
consumed number(4);
charge number(6,2);
ptype varchar2(10);
v_read number(5);
p_read number(5);
Begin
consumed := p_read-v_read;
if (ptype='res') then
charge := 60+(.75*consumed);
charge := 100+(2*consumed);
charge := 500+(3*consumed);
end if;
dbms_output.put_line(cid||' '||charge);
End;
OUTPUT
ELECTRICITY BILL
Cust_id Charge
12033 570
Exercise Program: 14
Create a Trigger and perform the following Queries (Creation of insert trigger, delete
trigger, update trigger).
Select * from customers;
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
+----+----------+-----+-----------+----------+
Exercise Program: 15
Write a PL/SQL program to demonstrate Packages.
Step1: Creating package specification
CREATE or replace PACKAGE emppackage AS
procedure emp_proc2;
function fname2(a in number,b in out number)
return number;
end;
Step2: Creating package definition or body
CREATE or replace PACKAGE body emppackage AS
procedure emp_proc2
is
begin
update emp
set sal=sal+sal*0.10
where comm<>sal*0.09;
DBMS_OUTPUT.PUT_LINE ('I am a procedure ');
end emp_proc2;
function fname2(a in number,b in out number)
return number is
begin
b:=a;
return b;
end fname2;
END;
Step3: Calling function / procedure of a package in a program
declare
a number:=10;
b number:=10;
begin
a:=emppackage.fname2(20,b);
dbms_output.put_line(a || b);
emppackage.emp_proc2;
end;
/
Output
2020
I am a procedure
PL/SQL procedure successfully completed.