You are on page 1of 8

Ex.

No: 12
Program to implement PL/SQL to show the uses of explicit cursor

(Use the Previously Created Customer Table )


DECLARE
c_id customers.id%type;
c_name customers.name%type;
c_addr customers.address%type;
CURSOR c_customers is
SELECT id, name, address FROM customers;
BEGIN
OPEN c_customers;
LOOP
FETCH c_customers into c_id, c_name, c_addr;
EXIT WHEN c_customers%notfound;
dbms_output.put_line(c_id || ' ' || c_name || ' ' || c_addr);
END LOOP;
CLOSE c_customers;
END;
/

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:

R: Rs 60 plus Rs 0.75 per kwh used

C: Rs 100 plus Rs 2 per kwh used

I: Rs 500 plus Rs 3 per kwh used

Use table with following attributes:

Column Name Format Remarks

Cons_no number(6) Primary key ,not Null

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.

3. Get the Cons_no to cid.

4. Calculate Consumed=p_read-v_read.

5. If type = „R‟ then

6. Charge = 60 + (.75*consumed)

7. Else if type = „C‟ then

8. Charge:=100+(2*consumed);

9. Else if type = „I‟ then

10. Charge:=500+(3*consumed);

11. Display cid and charge.

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.

SQL> Insert into Customer values(12003,‟Gowtham‟,‟R‟,‟230);

1 row created.

SQL> Insert into Customer values(12033,‟Shreehari‟,‟R‟,‟320);


1 row created.

SQL> Insert into Customer values(12034,‟Parthasarathy‟,‟C‟,‟500);

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

select pre_read,curr_read,type into v_read,p_read,ptype from electricity where cust_id=cid;

consumed := p_read-v_read;

if (ptype='res') then

charge := 60+(.75*consumed);

elsif (ptype='com') then

charge := 100+(2*consumed);

elsif (consumed<500) then

charge := 500+(3*consumed);

end if;

dbms_output.put_line(' ELECTRICITY BILL ');

dbms_output.put_line(' Cust_id Charge');

dbms_output.put_line(cid||' '||charge);
End;

OUTPUT

Enter value for cid: 12033

old 2: cid varchar2(10):='&cid';

new 2: cid varchar2(10):='c_101';

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 |
+----+----------+-----+-----------+----------+

CREATE OR REPLACE TRIGGER display_salary_changes


BEFORE DELETE OR INSERT OR UPDATE ON customers
FOR EACH ROW
WHEN (NEW.ID > 0)
DECLARE
sal_diff number;
BEGIN
sal_diff := :NEW.salary - :OLD.salary;
dbms_output.put_line('Old salary: ' || :OLD.salary);
dbms_output.put_line('New salary: ' || :NEW.salary);
dbms_output.put_line('Salary difference: ' || sal_diff);
END;
/
Trigger created.
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (7, 'Kriti', 22, 'HP', 7500.00 );
UPDATE customers
SET salary = salary + 500
WHERE id = 2;
Output:
Old salary: 1500
New salary: 2000
Salary difference: 500
DROP TRIGGER display_salary_changes;

Sample Program : Function


Write a PL/SQL program to demonstrate Functions.
SQL>
create or replace function fname(a in number,b in out number)
return number is
begin
b:=a;
return b;
end;
SQL> /
Function created.
Program to invoke a function
SQL>
declare
x number;
begin
x:=fname(23,x);
dbms_output.put_line(x);
end;
Output
SQL> /
23
PL/SQL procedure successfully completed.
Sample Program: Procedures
Write PL/SQL queries to create Procedures.
SQL>
create or replace procedure emp_proc1
IS
BEGIN
update emp
set sal=sal+sal*0.10
where comm<>sal*0.09;
END;
SQL> /
Procedure created.
Executing Procedure:
SQL> execute emp_proc1;
Output
PL/SQL procedure successfully completed.

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.

You might also like