Anda di halaman 1dari 10

PL SQL Program For adam or not.

declare n number(3); t1 number(3):=0; t2 number(3); s number(3):=0; revn number(3):=0; t number(3); begin n:=&n; t:=n; while(t>0) loop revn:=(revn*10)+(t mod 10); t:=trunc(t/10); end loop; dbms_output.put_line('Reversal of the number is ' ||revn); t1:=n*n; t2:=revn*revn; t:=t2; while(t>0) loop s:=(s*10)+(t mod 10); t:=trunc(t/10); end loop; if(s=t1) then dbms_output.put_line('The Given Number ' || n || ' is an Adam Number'); else dbms_output.put_line('The Given Number ' || n || ' is not an Adam Number'); end if; end;

PL SQL Program For to add two numbers. declare a number(2); b number(2); c number(2); begin a:=&a; b:=&b; c:=a+b; dbms_output.put_line(a|| ' + '||b||' = '||c); end;

PL SQL Program For to check the given number is armstrong or not. declare n number(3); s number(3):=0; t number(3); begin n:=&n; t:=n; while t>0 loop s:=s+power((t mod 10),3); t:=trunc(t/10); end loop; if(s=n) then dbms_output.put_line('The given number ' || n || 'is an armstrong number'); else dbms_output.put_line('The given number ' || n || 'is not an armstrong number'); end if; end;

PL SQL Program For to find biggest no. declare a number(2); b number(2); c number(2); begin a:=&a; b:=&b; c:=&c; if(a<b) then if(b<c) then dbms_output.put_line(c||' is the biggest number'); else dbms_output.put_line(b||' is the biggest number'); end if; else if(a<c) then dbms_output.put_line(c||' is the biggest number'); else dbms_output.put_line(a||' is the biggest number'); end if; end if; end;

PL SQL Program For counting record using trigger. create or replace trigger counts AFTER insert on TRISTUDENT declare c interger; begin select count(*) into c from tristudent; dbms_output.put_line('total records : '||c); end;

PL SQL Program For TO COMPUTE FACTORIAL. declare n number(2); i number(2); p number(2):=1; begin n:=&n; for i in 1..n loop p:=p*i; end loop; dbms_output.put_line(n ||' ! = '||p); end;

PL SQL Program For TO RETRIEVE THE RECORDS OF THE EMPLOYEE. declare no emp.empno %type; name emp.emp_name %type; sal emp.salary %type; begin no:=&no; select emp_name,salary into name,sal from emp where empno=no; dbms_output.put_line(' DETAILS OF THE EMPLOYEE WHOSE EMPLOYEE NO IS '|| no); dbms_output.put_line('Name of the Employee : ' || name); dbms_output.put_line('Salary of the Employee : ' || sal); exception when No_data_found then dbms_output.put_line('The Employee Number ' || no || ' Not Found'); end; PL SQL Program For EB AMOUNT CALCULATION. create or replace trigger calc After insert or update or delete on EBFIRST for each

row declare unit number(3); amt number(8,3); begin if inserting then unit:=:new.cur-:new.prev; if(unit>=500) then amt:=unit*5; elsif(unit>=400) then amt:=unit*4; elsif(unit>=300) then amt:=unit*3; elsif(unit>50) then amt:=unit*2; else amt:=50; end if; insert into ebsecond values(:new.cusno,:new.name,unit,amt); dbms_output.put_line(:new.cusno||' '||:new.name||' '||unit||' '||amt); dbms_output.put_line('amt calculated and records inserted'); end if; if updating then unit:=:new.cur-:new.prev; amt:=unit*2.5; update ebsecond set units=unit,amount=amt where cusno=:old.cusno; dbms_output.put_line(:old.cusno||' '||:old.name||' '||unit||' '||amt); dbms_output.put_line('amt calculated and records updated'); end if; if deleting then delete from ebsecond where cusno=:old.cusno; dbms_output.put_line(' Record '|| :old.cusno ||' deleted successfully'); end if; end;

PL SQL Program For handle user defined error. declare a number(3); b number(3); c number(3); divide_error exception; begin a:=&a; b:=&b; if(b=0) then raise divide_error; end if; c:=a/b; dbms_output.put_line('The Value of '||a||' / '||b||' is '||c);

exception when divide_error then dbms_output.put_line('A Number can not be divide by zero'); end;

PL SQL Program For Pre Defined Error. declare a number(3); b number(3); c number(3); begin a:=&a; b:=&b; c:=a/b; dbms_output.put_line('The Value of '||a||' / '||b||' is '||c); exception when zero_divide then dbms_output.put_line('DIVIDE BY ZERO ERROR'); end;

PL SQL Program For using Cursor. declare cursor c is select name,job,salary from employee; empname employee.name%type; jobs employee.job%type; sal employee.salary%type; begin open c; dbms_output.put_line(' NAME DESIGNATION SALARY'); loop fetch c into empname,jobs,sal; exit when c%notfound; dbms_output.put_line(empname||' '||jobs||' '||sal); end loop; close c; end;

PL SQL Program For function using exception. create or replace function stinfo(no in number) return varchar2 is snam

varchar2(30); begin select name into snam from student where stuid=no; return snam; exception when no_data_found then return(' Record does not exist '); end; PL SQL Program For function to return the grade of the student. create or replace function ret_grade(no in number) return varchar2 is grade varchar2(20); mark1 integer; mark2 integer; mark3 integer; average number(5,2); begin select m1,m2,m3 into mark1,mark2,mark3 from student where stuid=no; average:=(mark1+mark2+mark3)/3; if(mark1<50 or mark2<50 or mark3<50) then return('FAIL'); elsif(average>=90) then return('A+'); elsif(average>=80) then return('A'); elsif(average>=70) then return('B'); elsif(average>=60) then return('C'); else return('D'); end if; end; PL SQL Program For function to return the grade of the student using cursor. create or replace function gradeall return varchar2 is cursor c is select m1,m2,m3 from student; mark1 integer; mark2 integer; mark3 integer; average number(5,2); begin open c; loop fetch c into mark1,mark2,mark3; exit when c%notfound; average:=(mark1+mark2+mark3)/3; if(mark1<50 or mark2<50 or mark3<50) then return('FAIL');

elsif(average>=90) elsif(average>=80) elsif(average>=70) elsif(average>=60) else return('D'); end if; end loop; close c; end;

then then then then

return('A+'); return('A'); return('B'); return('C');

PL SQL Program For adding total using funtion. create or replace function fn(no in number)return varchar2 is msg varchar2(19); cursor c1 is select m1,m2,m3 from student where stuid=no; er c1 %rowtype; begin for er in c1 loop return ('total scored' ||(c1.m1+c1.m2+c1.m3)); end loop; end;

PL SQL Program For TO RETRIEVE THE NAME OF THE EMPLOYEE. CREATE OR REPLACE FUNCTION RETURN_NAME(NO NUMBER) RETURN VARCHAR2 IS NAM VARCHAR2(25); BEGIN SELECT NAME INTO NAM FROM EMPLOYEE WHERE EMPNO=NO; RETURN NAM; END;

PL SQL Program For invoice. declare no invoices.invno %type; actsale invoices.actualsales %type; ts invoices.targetsales %type; err exception; begin no:=&no; actsale:=&actsale;

ts:=&ts; if(actsale-ts <=0) then raise err; else insert into invoices(invno,actualsales,targetsales) values(no,actsale,ts); dbms_output.put_line('Commission '||(actsale-ts)*.25||' paid and record added sucessfully'); end if; exception when err then dbms_output.put_line('ACTUAL SALES SHOULD BE GREATER THAN TARGET SALES'); end;

PL SQL Program For to find the person whose getting maximum salary using cursor. declare cursor c is select name,job,salary from employee; empname employee.name%type; jobs employee.job%type; sal employee.salary%type; maxname employee.name%type:=0; maxjob employee.job%type:=0; maxsal employee.salary%type:=0; begin open c; fetch c into empname,jobs,sal; loop fetch c into empname,jobs,sal; if(maxsal maxsal:=sal; maxjob:=jobs; maxname:=empname; end if; exit when c%notfound; end loop; dbms_output.put_line('DETAILS OF THE PERSON GETTING MAXIMUM SALARY'); dbms_output.put_line('EMPLOYEE NAME : '||maxname); dbms_output.put_line('DESIGNATION : '||maxjob); dbms_output.put_line('SALARY : '||maxsal); close c; end;

PL SQL Program For to generate multiplication table. declare i number(2); n number(2); begin n:=&n; for i in 1..10 loop dbms_output.put_line( n || ' * ' || i || ' = ' || n*i); end loop; end;

PL SQL Program For to store odd date and even date records on the separate table. declare ID INVOICE.INVNO%TYPE; MOVIENO INVOICE.MVNO%TYPE; CID INVOICE.CUSID%TYPE; DAT1 INVOICE.ISSDATE%TYPE; DAT2 INVOICE.RETDATE%TYPE; t integer; CURSOR C IS SELECT * FROM INVOICE; BEGIN OPEN C; delete from odd; delete from even; LOOP FETCH C INTO ID,MOVIENO,CID,DAT1,DAT2; exit when c%notfound; t:=to_char(DAT1,'dd'); IF t MOD 2=0 THEN INSERT INTO even(ID,MVNUM,CID,ISSUE,RETURN) VALUES(ID,MOVIENO,CID,DAT1,DAT2); ELSE INSERT INTO ODD(ID,MVNUM,CID,ISSUE,RETURN) VALUES(ID,MOVIENO,CID,DAT1,DAT2); end if; end loop; close c; end;

PL SQL Program For Preating Package.

create or replace package prcpack is procedure ins(no in number,nam in varchar2); procedure delt(no in number); procedure updat(no number,nam varchar2); end;

PL SQL Program For to insert records using procedures. create or replace package body prcpack is procedure ins(no in number,nam in varchar2) is begin insert into employ values(no,nam); dbms_output.put_line(' Record inserted successfully'); end ins; procedure delt(no in number) IS begin delete from employ where empid=no; dbms_output.put_line(' Record '||no||' deleted succesfullly'); end delt; procedure updat(no in number,nam varchar2) IS begin update employ set empname=nam where empid=no; dbms_output.put_line(' Record '||no||' updated succesfullly'); end updat; end;

PL/SQL PROGRAM FOR TO DELETE RECORDS USING PROCEDURE. create or replace procedure prcupdate(id in number,nam in varchar2) is row employ%rowtype; begin dbms_output.put_line(' Before Updation'); select * into row from employ where empid=id; dbms_output.put_line('EMP ID : '||row.empid||' EMPNAME : '||row.empname); update employ set empname=nam where empid=id; COMMIT; dbms_output.put_line('Updated Record'); select * into row from employ where empid=id; dbms_output.put_line('EMP ID : '||row.empid||' EMPNAME : '||row.empname); end;

Anda mungkin juga menyukai