PL/SQL Programs: 1. Search For Student Name Using Student Number
PL/SQL Programs: 1. Search For Student Name Using Student Number
PL/SQL Programs: 1. Search For Student Name Using Student Number
Aim: To write a pl/sql program to give student No search for student and if found display first name and last name in Cyber College. Program: SQL > DECLARE St_id char(9); Lname varchar2(30); Fname varchar2(30); BEGIN St_id:=& Stud_id; Select Student_Lastname, Student_Firstname into lname,fname from Student where Student_Number = St_id; DBMS_OUTPUT.PUT_LINE (Name is || fname || || lname); END; 2. USE TRIM FUNCTIONS
Aim: To write a pl/sql program to give student No search for student and if found display first name and last name in Cyber College and eliminate unnecessary blank space to the right. Program: SQL > DECLARE St_id char (9);
Lname varchar2 (30); Fname varchar2 (30); BEGIN St_id:=& Stud_id; Select Student_Lastname, Student_Firstname into lname,fname from Student where Student_Number = St_id; DBMS_OUTPUT.PUT_LINE( Name is ||Rtrim( fname) || || Rtrim( lname); END; 3. ERROR HANDLING Aim: To write a pl/sql program to give student No search for student and if found display first name and last name in Cyber College and adding error handling section. Program: SQL > DECLARE St_id char (9); Lname varchar2 (30); Fname varchar2 (30); BEGIN St_id:=& Stud_id; Select Student_Lastname, Student_Firstname into lname,fname from Student where Student_Number = St_id; DBMS_OUTPUT.PUT_LINE( Name is || fname || || lname); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE(Please Check Your Student ID);
END; 4. PRINT STATE OF STUDENT USING IF.ELSE CONTRUCT Aim: To write a pl/sql program to Print out the state to which each student belongs, suppose the same field TX then the output should be TEXAS RESIDENT ( use if . else constructor).
Program:
SQL > DECLARE St_id char(9); name varchar2(30); BEGIN St_id:=& Stud_id; Select State into name from Student where Student_Number=St_id;
IF name = TX THEN DBMS_OUTPUT.PUT_LINE (State is TESTUDROWAS RESIDENT); ELSEIF name=OH THEN DBMS_OUTPUT.PUT_LINE (State is OHIO RESIDENT); ELSEIF name=FL THEN DBMS_OUTPUT.PUT_LINE (State is FLORIDA RESIDENT); ELSEIF name=GA THEN DBMS_OUTPUT.PUT_LINE (State is GWALIOR RESIDENT);
END;
5. SUM OF SERIES USING FOR LOOP Aim: To write pl/sql program to calculate the sum of series input given are starting and ending values. Program: SQL > DECLARE
i number (2); s number (2); n number (2); som numbers (3); BEGIN som:= 0; s:= &s; FOR i IN s..n
LOOP Som:= som + I; END LOOP; DBMS_OUTPUT.PUT_LINE (Sum OF Numbers || som); END;
6. PRINT SERIES OF NUMBER USING FOR LOOP Aim: To write a pl/sql program to print the series of numbers, Input given are starting number, ending number values.
Program: SQL > DECLARE i number(2); s number(2); n number(2); BEGIN s:=& startnumber; n:=& endnumber DBMS_OUTPUT.PUT_LINE (Series of numbers); FOR i IN s n LOOP DBMS_OUTPUT.PUT_LINE( i ); END LOOP END;
7. PALINDROME OR NOT Aim: To write a pl/sql program to check whether the name is palindrome or not. Program: SQL > DECLARE name varchar2(30); l number(3); i number(2); j number(2); BEGIN name:= &name ; l:=length(name); j:=l; FOR i IN 1...l LOOP IF substr(name,i,1) = substr(name,j,1) THEN j := j -1; ELSE EXIT; END IF; END LOOP; IF j=0 THEN DBMS_OUTPUT.PUT_LINE (name || is palindrome); ELSE DBMS_OUTPUT.PUT_LINE (name || is not palindrome); END IF
EXCEPTION WHEN NO_DATA _FOUND THEN DBMS_OUTPUT.PUT_LINE (Please Check Ur Student ID); END; 8. STORED PROCEDURE TO DISPLAY STUDENT INFORMATION Aim: To create a stored procedure to display student information. Input given is student number. Program: CREATE OR REPLACE PROCEDURE studInfo (NO IN NUMBER) AS Lname varchar2 (30); STA varchar2 (10); BEGIN SELECT Student_Lastname, State into Lname, STA from Student where Student_number=NO; DBMS_OUTPUT.PUT_LINE (Student Name || Lname); DBMS_OUTPUT.PUT_LINE (State || STA); EXCEPTION When No_Data_Found THEN DBMS_OUTPUT.PUT_LINE (Data Is Not Found); END;
9. DISPLAY Aim:
AVAILABLE
SEATS
To create a stored procedure to display the available seats in a section. Input given is section id , create separate function to calculate available.
Function: CREATE OR REPALCE FUNCTION AVAILABLE ( t In number , e In number) RETURN number AS tot(number(3); BEGIN tot:=t e; RETURN tot; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE (Data Not Found); END: Procedure: CREATE OR REPALCE PROCEDURE AVAILABLE ( id In number , tim In number) AS avai number(3); tot(number(3); enroll number(3); BEGIN Select Class_size , Number_enrolled into tot, enroll FROM Section Where Section_id= id and Time_offered=tim; DBMS_OUTPUT.PUT_LINE (tot || enroll); avai := Available(tot,enroll); DBMS_OUTPUT.PUT_LINE (avai || Seats avalible in section || id); EXCEPTION WHEN NO_DATA_FOUND THEN
10. DISPLAY STUDENT INFORMATION Aim: To create a trigger to update total enrolled number while issue insert and delete statement on table enrollment. Trigger: CREATE OR REPLACE TRIGGER tri_enroll after insert or delete on Enrollment for each row. BEGIN IF INSERTING THEN update Section set Number_Enrolled = Number_Enrolled+1 Where Section_id=:new.section_id; DBMS_OUTPUT.PUT_LINE(UPDATED); ELSE IF DELETING THEN update Section set Number_Enrolled = Number_Enrolled-1 Where Section_id=:old.section_id; END IF; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE(Data Not Found); END: 11. ELEGIBLE FOR VOTING Aim: To display people who are eligible for voting from a list of residents in a colony, use cursor. Procedure:
DECLARE cursor cur_vot is select p_name,plot_number from People Where MONTHS_BETWEEN(SYSDATE,DOB)>=216; peorow cur_vot % ROWTYPE; name People , p_name % TYPE; no People, plot_number % TYPE; c number(5); BEGIN OPEN cur_vot; c:=0; FETCH cur_vot into name , no; DBMS_OUTPUT.PUT_LINE(NAME Plot NO);
DBMS_OUTPUT.PUT_LINE(-----------------------------); WHILE c < cur_vot % ROWCOUNT LOOP DBMS_OUTPUT.PUT_LINE(name || FETCH cur_vot into name,no; c:=c+1; END LOOP; CLOSE cur_vot; END; || no);
Aim: To calculate total marks, percentage of marks and grade for our class, use cursor.
Procedure: DECLARE CURSOR cur_sta IS Select name, mark1, mark2, mark3, mark4, mark5 from stu; sturow cur_stu % ROWTYPE; na stu.name % TYPE; m1 stu.mark1 % TYPE; m2 stu.mark2 % TYPE; m3 stu.mark3 % TYPE; m4 stu.mark4 % TYPE; m5 stu.mark5 % TYPE; tot number(5); av number(5,2); gr char(1); c number(5); BEGIN OPEN cur_stu; c:=0; FETCH cur_stu into na, m1, m2, m3, m4, m5; DBMS_OUTPUT.PUT_LINE (NAME DBMS_OUTPUT.PUT_LINE (------WHILE c < cur_stu % ROWCOUNT LOOP tot:=m1+m2+m3+m4+m5; av:= tot/5; IF av >= 75 THEN TOTAL --------PERCENTAGE GRADE);
-------------- -----------);l
gr:=A; ELSE IF av >=65 AND av < 75 THEN gr:=B; ELSE IF av >=50 AND av < 65 THEN gr:=C; ELSE gr:=D; END IF DBMS_OUTPUT.PUT_LINE(na || || tot || || av || || || gr); FETCH cur_stu into na, m1, m2, m3, m4, m5; c:= c+1; END LOOP; CLOSE cur_stu; END;
13. INTEREST TO CUSTOMER Aim: Write a pl/sql program to give 10% interest to customer belonging to branch located in the given branch city if their deposit amount limits value. Input given are branch city, name and amount limit. List out the full details of customer from the given branch. Procedure: DECLARE bn varchar2(20); am number(10,2); BEGIN DBMS_OUTPUT.PUT_LINE(Enter branch name);
bn:= & branchname; DBMS_OUTPUT.PUT_LINE (Enter amount); am:=&amount; update Deposit set Amount = amount * (Amount (10/100)) where Deposit.BranchName in (select Branch,BranchName from branch where Branch.city=bn) and Amount > am; DBMS_OUTPUT.PUT_LINE (Updated); END;
14. INTEREST TO CUSTOMER Aim: Write a pl/sql program to give 10% interest to customer in the given branch name who deposited after the year of deposit value, input given are year of deposit and branch name. Procedure: DECLARE bn varchar2(20); ded date; BEGIN DBMS_OUTPUT.PUT_LINE (Enter branch name); bn:= & branchname; DBMS_OUTPUT.PUT_LINE (Enter date of deposit); ded:=&Depositedate; update Deposit set Amount = amount * (Amount (10/100)) where Deposit.BranchName = bn and MONTH_BETWEEN(Date of Deposit,ded) >0; DBMS_OUTPUT.PUT_LINE (Updated); END;
15. NUMBER OF CUSTOMER IN BRANCH Aim: Write a pl/sql program to list name of branches and the total number of customer in each branch where the sum of the deposit is more than 5000. Program: DECLARE CURSOR cur_bname IS group by select branchname , sum(Amount),count(AccountNo) from deposit branchname having sum(Amount)>5000; bna deposit.branchnamr % TYPE; am deposit.amount % TYPE; c number(3); cn number(3); BEGIN OPEN cur_bname; c:=0; FETCH cur_bnae into bna,am,cn; DBMS_OUTPUT.PUT_LINE (Branch Name No of Customers); DBMS_OUTPUT.PUT_LINE (-------------------Sum of deposit --------);
WHILE c < cur_bname % ROWCOUNT LOOP DBMS_OUTPUT.PUT_LINE (bna || FETCH cur_bname into bna,am,cn; c:=c+1; END LOOP; || am || || cn);
END;
16. NUMBER OF CUSTOMER IN BRANCH Aim: Write a pl/sql program to list name of branches and the total number of customer in each branch where the sum of the deposit is less than 2000. Program: DECLARE CURSOR cur_bname IS group by select branchname , sum(Amount),count(AccountNo) from deposit branchname having sum(Amount)< 2000; bna deposit.branchnamr % TYPE; am deposit.amount % TYPE; c number(3); cn number(3); BEGIN OPEN cur_bname; c:=0; FETCH cur_bnae into bna,am,cn; DBMS_OUTPUT.PUT_LINE (Branch Name No of Customers); DBMS_OUTPUT.PUT_LINE (-------------------Sum of deposit --------);
WHILE c < cur_bname % ROWCOUNT LOOP DBMS_OUTPUT.PUT_LINE(bna || FETCH cur_bname into bna,am,cn; c:=c+1; || am || || cn);
17. ADD RS. 100 TO DEPOSITOR IN GIVEN CITY Aim: Write a pl/sql program to add Rs.100 to the customer who having an account in branch located in that city, give the name of the city which has the maximum of branches. Program: DECLARE CURSOR cur_bname IS Select branchname from branch where city in(Select city from branch group by city having count(branchname) = (Select max(count(branchname)) from branch group by city)); bna branch.branchname % TYPE; c number(3); BEGIN OPEN cur_bname; c:=0; FETCH cur_bname into bna; WHILE c < cur_bname into bna; LOOP update deposit set amount=amount +100 where branchname =bna; FETCH cur_bname into bna; c:=c+1; END LOOP; END;
18. FIND MAXIMUM NUMBER OF CUSTOMER IN THE CITY Aim: To find out the city in which the maximum number of customer alive. List out the branches in that particular city. Program: DECLARE CURSOR cur_city IS select branchname from branch where city in (select city from Customer group by city having count(Customername) >= (select max(count(Customername)) from Customer group by city)); can Customer.city % TYPE; c number(3); BEGIN OPEN cur_city; c:=0; FETCH cur_city into can; DBMS_OUTPUT.PUT_LINE(Branch Names of city which has maximum customers); WHILE c < cur_city % ROWCOUNT LOOP DBMS_OUTPUT.PUT_LINE( || cna); FETCH cur_city into can; c :=c+1; END LOOP; END;
Aim: To write a pl/sql program to accept three subject marks out of 50 and name of the student. List out the name of the students who have scored more than 80% in all subjects. Program: DECLARE CURSOR cur_stu IS Select Name from students where mark1>80 and mark2>80 and mark3>80; nam students.name % TYPE; rn students.name % TYPE; m1 students.mark1 % TYPE; m2 students.mark2 % TYPE; m3 students.mark3 % TYPE; i number(3); c number(3); BEGIN FOR i in 1 .. 50 LOOP rn:= &rollno; nam:=&name; m1:=&m1; m2:=&m2; m3:=&m3; INSERT into students values(an,m1,m2,m3); END LOOP; OPEN cur_stu;
c:=0; FETCH cur_stu into nam; DBMS_OUTPUT.PUT_LINE(Student who having more than 80%); DBMS_OUTPUT.PUT_LINE(--------------------------------------------); WHILE c < cur_stu % ROWCOUNT LOOP DBMS_OUTPUT.PUT_LINE( || nam); FETCH cur_stu into nam; c:=c+1; END LOOP; END;
20. CHECKING FOR LOAN OR DEPOSIT OF A CUSTOMER Aim: Display whether a customer has token loan and if loan taken check whether he has an account also, input given is customer name. Program: DECLARE nam varchar2(20); ln number(5); ac number(5); CURSOR cur_cus IS Select AccountNo from deposit where Customername=nam; CURSOR cur_bor IS Select LoanNo from borrow where Customername=nam; BEGIN nam:=&name;
OPEN cur_cus; FETCH cur_cus into ac; IF cur_cus % ROWCOUNT >=1 THEN OPEN cur_bor; FETCH cur_bor into ln; IF cur_bor % ROWCOUNT >=1 THEN DBMS_OUTPUT.PUT_LINE (nam || has both account and loan); ELSE DBMS_OUTPUT.PUT_LINE (nam || has account but has no loan); END IF; ELSE DBMS_OUTPUT.PUT_LINE ( Has no record for || nam); END IF; END;
---------------------------------------------------------------------------------------------------------