Write A PL/SQL Program To Implement A Simple Calculator

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Aakar Gupta

19BCE0379

1. Write a PL/SQL program to implement a simple calculator.


DECLARE
a
number;
b
number;
sum1
number; mul
number; div
number; sub
number;
x
number;
y
number;
BEGIN
a:= &x;
b:=&y;
sum1:=a+
b; sub:= a-
b; mul:=
a*b; div:=
a/b;

dbms_output.put_line('Sum of a and b is: '||sum1);


dbms_output.put_line('Multiplication of a and b is:
'||mul); dbms_output.put_line('Division of a and b is:
'||div); dbms_output.put_line('Subtraction of a and b is:
'||sub); END;
/

Cycle Sheet- 3 Page 1


Aakar Gupta
19BCE0379

2. Write a PL/SQL program to practice reading the record from a table into local
variables using different data types and %TYPE and display the same using locally
declared variables.

DECLARE
d_id Doctor.Doc_Id%type :=
'D0001'; d_name
Doctor.Doc_name%type; gen
Doctor.gender%type;
dob_d Doctor.dob%type ;
spec_d
Doctor.specialist%type;
qual_d

Cycle Sheet- 3 Page 2


Aakar Gupta
19BCE0379

Doctor.qualification%type;
Con_d Doctor.Contact%type ;
Add_d Doctor.Address%type ;

dept_d

Doctor.dept_no%type;

BEGIN

SELECT Doc_Id, Doc_name, gender, dob, specialist, qualification,Contact,


Address, dept_no INTO d_id, d_name, gen, dob_d, spec_d, qual_d, Con_d, Add_d,
dept_d
FROM Doctor
WHERE Doc_id =
d_id;
dbms_output.put_lin
e
('Readed value Output of Doctor table:' || d_id||' ' || d_name ||' ' || gen || ' ' ||dob_d ||' ' ||
spec_d || ' ' ||qual_d ||' ' || Con_d ||' ' || Add_d ||' ' || dept_d);
END;
/

Cycle Sheet- 3 Page 3


Aakar Gupta
19BCE0379

3. Write a PL/SQL program to find the number of doctors in a given department with a
given qualification (read values for department and qualification from user during
runtime). If number is more than the number of doctors in that department with other
qualifications then display ‘Well qualified’ else ‘Qualified’.

DECLARE
deptno_var
doctor.dept_no%type; quali_var
doctor.qualification%type;
answer number;
doctorn
number;
BEGIN
deptno_var:=&departmentnumber
quali_var:=&qualification;
Select COUNT(*) into answer from doctor where dept_no=deptno_var AND qualification
=quali_var; Select COUNT(*) into doctorn from doctor;
Dbms_output.put_line('Number of doctors in the given department and qualification:' ||
answer); Dbms_output.put_line('Total number of doctors:' || doctorn);
If(answer>doctorn) then
Dbms_output.put_line('Well Qualified');
Else
Dbms_output.put_line('Qualified');
End
if;
end;
/

Cycle Sheet- 3 Page 4


Aakar Gupta
19BCE0379

4. Write a PL/SQL program to insert records into any of the tables in your database.

insert into Department values('DP012','Diabetes',501,5,D0012,'28-Nov-


2011'); BEGIN
INSERT INTO Doctor
values('D0012','Cypher','M','28-Jun-
2001','Diabetes','MBBS',84412850,'Delhi','DP012');
END;

Cycle Sheet- 3 Page 5


Aakar Gupta
19BCE0379

5. Create a function to find the factorial of a given number.

CREATE FUNCTION factorial(x


number) RETURN number
IS
f
number;
BEGIN
IF x=0
THEN f :=
1;
ELSE
f := x * factorial(x-
1); END IF;
RETURN
f; END;
/

Cycle Sheet- 3 Page 6


Aakar Gupta
19BCE0379

DECLARE
num
number; fact
number;
BEGIN
num:= 8;

fact := factorial(num);
dbms_output.put_line(' Factorial of '|| num || ' is ' ||
factorial(num)); END;
/

6. Create a function DOC_COUNT to find the number of doctors in the given


department. Use the department name as the input parameter for the function.
CREATE FUNCTION DOC_COUNT(Dpt_NAME VARCHAR2)
RETURN
number IS
cont
number;
BEGIN
SELECT COUNT(1) INTO cont FROM DOCTOR
INNER JOIN DEPARTMENT ON
DOCTOR.DEPT_NO=DEPARTMENT.DEPT_NO WHERE
DEPARTMENT.DEPT_NAME=Dpt_NAME;
RETURN
cont; END;

Cycle Sheet- 3 Page 7


Aakar Gupta
19BCE0379

DECLARE
Dpt_NAME VARCHAR2(10);
Doc_cont
number;
BEGIN

Dpt_NAME:= 'Diabetes';
Doc_cont := DOC_COUNT(Dpt_NAME);
dbms_output.put_line(' Total number of doctors in '|| Dpt_NAME || ' is '
|| DOC_COUNT(Dpt_NAME));
END;
/

Cycle Sheet- 3 Page 8


Aakar Gupta
19BCE0379

Cursors:
1. Write a CURSOR to give 5% additional discount to all senior citizen patients.

DECLARE
Id
patient.PAT_ID%type;
age number(3,1);
CURSOR hospitalbill_cur is select pat_id from
patient; BEGIN

Open
hospitalbill_cur;
LOOP
FETCH hospitalbill_cur into id;
select (sysdate-dob)/365 into age from patient where
pat_id= id; EXIT WHEN hospitalbill_cur%notfound;
IF(age >60) then
update
Hospital_bill
set
discount=discount+5
where pat_id=id;
END IF;
END
LOOP;
END;

Cycle Sheet- 3 Page 9


Aakar Gupta
19BCE0379

2. Write a CURSOR to change the department number from 1 as 5 for all doctors with a
qualification
‘MD’ Set
serveroutput on
DECLARE
Qualification_var
doctor.qualification%type; id
doctor.doc_id%type

CURSOR department_cur is select doc_id,qualification from doctor where


dept_no=’D0001’; BEGIN
Open
department_cur;
LOOP
FETCH department_cur into id,
qualification_var; EXIT when
department_cur%notfound;
IF(qualification_var ='MD')
then update doctor
set

Cycle Sheet- 3 Page 10


Aakar Gupta
19BCE0379

dept_no='D0005'
where doc_id= id;
END IF;
END
LOOP;
END;

Cycle Sheet- 3 Page 11


Aakar Gupta
19BCE0379

Cycle Sheet- 3 Page 12


Aakar Gupta
19BCE0379

Functions and Procedures:


1. Write a PL/SQL stored function COUNT_DOC to count the number of doctors who
have treated at least 100 patients if given a doctor id as input parameter.

create or replace function COUNT_DOC(x in


char) return number is
count_var number;
begin
select count(*)
into count_var from
appointment where doc_id= x;
if count_var>= 100 then
count_var := count_var+ 1;
else
count_var := count_var+0;
end if;
return count_var
; end;

declare
ans
number(1); x
char;
begin
ans := COUNT_DOC(&x);
dbms_output.put_line('Number of Doctors treated more than 100 patients : ' ||
ans); End;
/

Cycle Sheet- 3 Page 13


Aakar Gupta
19BCE0379

2. Write a PL/SQL stored procedure to adjust the payment type of hospital bills to
CASH if the patient id and amount details given as input.
create or replace procedure paymentchange(a in char, b in
number) is
HosBill
hospital_bill%ROWTYPE;
begin
update hospital_bill set Payment_type = 'Cash' where Pat_ID = a and Bill_Amount = b;
dbms_output.put_line('Patient id: ' || HosBill.Pat_ID || ' ' || 'Bill Amount: ' ||
HosBill.Bill_Amount|| ' ' || 'Payment Type: ' ||HosBill.Payment_Type);
exception
when others then
dbms_output.put_line(SQLERR
M); End;
/

Cycle Sheet- 3 Page 14


Aakar Gupta
19BCE0379

execute
paymentchange('P0001',200000);
execute
paymentchange('P0002',300000);

Cycle Sheet- 3 Page 15


Aakar Gupta
19BCE0379

Triggers:
Add an attribute with patients table to store the age of the patients. Then answer
the following question;
1. Write a Trigger to find and fill the age of a patient whenever a patient record is
inserted into patients table.
alter table patient add age int;

create or replace trigger calculate_age before insert on patient for


each row begin
:new.age :=
months_between(sysdate,:new.DOB)/12; end;
/

insert into patient(pat_id,pat_name,DOB,Gender,Contact,Address)


values('P0007','Rishabh','23- MAY-2001','M',9848578958,'Agra');
select * from patient where pat_id='P0007';

Cycle Sheet- 3 Page 16


Aakar Gupta
19BCE0379

Create a table EMP_SALARY with attributes ID, Basic, DA, HRA, Deduction,
Net_Salary. Here, ID refers the Staff_ID of staff table. Treat ‘Net_Salary’ as a derived
attribute and don’t insert a value through insert operation. The value for Net Salary
can be calculated as follows;
Net_Salary = Basic + DA + HRA – Deduction
1. Write a Trigger to perform the following; whenever new staff is recruited and a
designation is assigned, insert an appropriate record into EMP_SALARY table. Refer
the following table for salary details.

CREATE TABLE EMP_SALARY (


id varchar(20),
basic
number(20), da
number(20), hra
number(20),
deduction float,
net_salary float,
constraint ref_staff_id foreign key(id) references Staff(staff_id)
);

insert into emp_salary(id,basic,da,hra,deduction)


values ('S0001',6000,2000,2000,2);
insert into emp_salary(id,basic,da,hra,deduction)
values ('S0002',8000,2500,3000,2);
insert into emp_salary(id,basic,da,hra,deduction)
values ('S0003',6000,2000,2000,2);
insert into emp_salary(id,basic,da,hra,deduction)
values ('S0004',9000,2500,3500,2.5);
insert into emp_salary(id,basic,da,hra,deduction) values

Cycle Sheet- 3 Page 17


Aakar Gupta
19BCE0379

('S0005',5000,1500,2000,2);
insert into emp_salary(id,basic,da,hra,deduction)
values ('S0006',6500,2000,2000,2);

CREATE OR REPLACE TRIGGER netsalary before INSERT OR UPDATE ON


EMP_SALARY FOR EACH ROW BEGIN
:new.net_salary := :new.basic + :new.da + :new.hra -
(((:new.deduction)*(:new.basic))/100); END;
/
Select id,net_salary,basic, da, hra, deduction ,desgination from EMP_SALARY join
Staff on id = staff_id;

Cycle Sheet- 3 Page 18


Aakar Gupta
19BCE0379

Cycle Sheet- 3 Page 19

You might also like