QUESTIONS: 1. Write a PL/SQL program to implement a simple calculator Code: DECLARE x INT; y INT; ch CHAR(1); z INT:=0; BEGIN x :=&x; ch :='&ch'; y :=&y; CASE ch WHEN '+' THEN z:=x+y; WHEN '-' THEN z:=x-y; WHEN '*' THEN z:=x*y; WHEN '/' THEN z:=x/y; ELSE DBMS_OUTPUT.PUT_LINE('INVALID'); END CASE; DBMS_OUTPUT.PUT_LINE(x||ch||y||'='||z); END; / 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. Code: set serveroutput on; DECLARE deptno department.dept_no%type; deptname department.dept_name%type; deptroom department.room_no%type; deptfloor department.floor%type; depthod department.hod%type; deptestd department.estd_date%type; BEGIN select dept_no,dept_name,room_no,floor,hod,estd_date into deptno,deptname,deptroom,deptfloor,depthod,deptestd from department where dept_no='&dept_no'; DBMS_OUTPUT.PUT_LINE('department_no= '||deptno); DBMS_OUTPUT.PUT_LINE('department_name= '||deptname); DBMS_OUTPUT.PUT_LINE('department_room= '||deptroom); DBMS_OUTPUT.PUT_LINE('department_floor= '||deptfloor); DBMS_OUTPUT.PUT_LINE('department_hod= '||depthod); DBMS_OUTPUT.PUT_LINE('department_estd_date= '||deptestd); END; / 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’. Code: set serveroutput on; DECLARE x int; y int; dept doctor.dept_no%type; qual doctor.qualification%type; BEGIN dept:=’&dept’; qual:='&qual'; SELECT COUNT (Doc_id) into x from Doctor where Dept_no=dept and Qualification=qual; SELECT COUNT (Doc_id) into y from Doctor where Dept_no=dept and Qualification!=qual; DBMS_OUTPUT.PUT_LINE('Total no of Doctors in given department with given qualification are '||x); if x>y 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. Code: set serveroutput on; DECLARE pid patient.Pat_id%type; pname patient.Pat_name%type; dob patient.DOB%type; gender patient.gender%type; cont patient.contact%type; add patient.address%type; BEGIN pid:='&pid'; pname:='&pname'; dob:='&dob'; gender:='&gender'; cont:='&cont'; add:='&add'; insert into patient values(pid,pname,dob,gender,cont,add); commit; END; / 5. Create a function to find the factorial of a given number. Code: set serveroutput on; create or replace function fact(n number) return number is i number; ans number; begin ans:=1; for i in 1 .. n loop ans := ans*i; end loop; return ans; end; / set serveroutput on; declare n number; begin n:=fact(&n); DBMS_OUTPUT.PUT_LINE(chr(10)||'Factorial is: '||n); 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. Code: set serveroutput on; create or replace function DOC_COUNT(deptname VARCHAR) return INT is n number; begin select count(Doc_id) into n from doctor,department where doctor.dept_no=department.dept_no and dept_name=deptname; return n; end; / set serveroutput on; declare n number; deptname department.dept_name%type;* begin deptname:='&deptname'; n:=DOC_COUNT(deptname); DBMS_OUTPUT.PUT_LINE('No. of Doctors in given Department : '||n); end; / CURSOR 1. Write a CURSOR to give 5% additional discount to all senior citizen patients. Code: set serveroutput on; DECLARE cursor pt is select * from patient; p pt%rowtype; age decimal(5,2); BEGIN open pt; loop fetch pt into p; exit when pt%notfound; age:=months_between(sysdate,p.dob)/12; if(age>=60) then update hospital_bill set discount=discount+5 where pat_id=p.pat_id; end if; end loop; close pt; END; / 2. Write a CURSOR to change the department number from 1 as 5 for all doctors with a qualification ‘MD’ Code: set serveroutput on; DECLARE cursor doc is select * from doctor; dr doc%rowtype; n int:=0; BEGIN open doc; loop fetch doc into dr; exit when doc%notfound; if(dr.qualification='MD') AND (dr.dept_no=’D101’) then update doctor set dept_no=’D105’ where doc_id=dr.doc_id; select dept_no into n from doctor where doc_id=dr.doc_id; DBMS_OUTPUT.PUT_LINE('New Dept_no= '||n); end if; end loop; close doc; END; / 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. Code: set serveroutput on; create or replace function COUNT_DOC(dr_id char) return number is n number; begin select count(Pat_id) into n from Appointment where Doc_id=dr_id; if (n>=100) then return 1; else return 0; end if; end; / set serveroutput on; DECLARE n number; total int:=0; cursor doc is select * from doctor; dr doc%rowtype; BEGIN open doc; loop fetch doc into dr; exit when doc%notfound; n:=COUNT_DOC(dr.doc_id); total:=total+n; end loop; close doc; DBMS_OUTPUT.PUT_LINE('Total no of Doctors= '||total); 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. Code: set serveroutput on; create or replace procedure pttype(pt_id char,amt number) is n hospital_bill.payment_type%type; begin update hospital_bill set payment_type='Cash' where pat_id=pt_id and bill_amount=amt; select payment_type into n from hospital_bill where pat_id=pt_id and bill_amount=amt; DBMS_OUTPUT.PUT_LINE('New Payment_Type= '||n); end; / exec pttype('&pt_id',&amt); TRIGGERS 1. Add an attribute with patients table to store the age of the patients. Then answer the following question; Write a Trigger to find and fill the age of a patient whenever a patient record is inserted into patients table. Code: alter table Patient add age int; create or replace trigger calc_age before insert on patient for each row begin :new.age := months_between(sysdate,:new.dob)/12; end; / insert into patient values('P200','SHIPRA','10-Feb-2002','F',7800923938,'Manali Road',''); select * from patient where pat_id='P200'; 2. 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 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. Code: CREATE TABLE EMP_SALARY( ID VARCHAR(4) PRIMARY KEY, BASIC NUMBER NOT NULL, DA NUMBER NOT NULL, HRA NUMBER NOT NULL, DEDUCTION DECIMAL(2,1) NOT NULL, NET_SALARY NUMBER NOT NULL ); create or replace trigger Staff_info after insert on Staff for each row begin CASE :new.designation WHEN 'Staff nurse' THEN insert into EMP_SALARY VALUES(:new.staff_id,6000,2000,2000,2,''); WHEN 'Head nurse' THEN insert into EMP_SALARY VALUES(:new.staff_id,8000,2500,3000,2,''); WHEN 'Technician' THEN insert into EMP_SALARY VALUES(:new.staff_id,6000,2000,2000,2,''); WHEN 'Senior technician' THEN insert into EMP_SALARY VALUES(:new.staff_id,9000,2500,3500,2.5,''); WHEN 'Junior attender' THEN insert into EMP_SALARY VALUES(:new.staff_id,5000,1500,2000,2,''); WHEN 'Senior attender' THEN insert into EMP_SALARY VALUES(:new.staff_id,6500,2000,2000,2,''); END CASE; end; / create or replace trigger salary_info before insert on EMP_SALARY for each row begin :new.net_salary := :new.BASIC+:new.DA+:new.HRA(:new.DEDUCTION/100*:new.BASIC); end; / insert into Staff values('S016','Aditi','Nurse','Staff nurse','10-Apr1997',9010262645,'Railway Road',’D201’); select * from EMP_SALARY;