Professional Documents
Culture Documents
Write A PL/SQL Program To Implement A Simple Calculator
Write A PL/SQL Program To Implement A Simple Calculator
Write A PL/SQL Program To Implement A Simple Calculator
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
Doctor.qualification%type;
Con_d Doctor.Contact%type ;
Add_d Doctor.Address%type ;
dept_d
Doctor.dept_no%type;
BEGIN
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;
/
4. Write a PL/SQL program to insert records into any of the tables in your database.
DECLARE
num
number; fact
number;
BEGIN
num:= 8;
fact := factorial(num);
dbms_output.put_line(' Factorial of '|| num || ' is ' ||
factorial(num)); END;
/
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;
/
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;
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
dept_no='D0005'
where doc_id= id;
END IF;
END
LOOP;
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;
/
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;
/
execute
paymentchange('P0001',200000);
execute
paymentchange('P0002',300000);
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 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.
('S0005',5000,1500,2000,2);
insert into emp_salary(id,basic,da,hra,deduction)
values ('S0006',6500,2000,2000,2);