# Ex. No.

:5
1.odd or even

declare n number; begin n:=&num; if mod(n,2)=0 then DBMS_OUTPUT.PUT_LINE('The number is even'); else DBMS_OUTPUT.PUT_LINE('The number is odd'); end if; end; 2. fibbonacci declare a number(2); b number(2); n number(2); i number(2); c number(2); begin a:=-1; b:=1; i:=1; n:=&number; loop c:=a+b; a:=b; b:=c; DBMS_OUTPUT.PUT_LINE(c); exit when i=n; i:=i+1; end loop; end;
old 11: n:=&number; new 11: n:=6; 0 1 1 2 3 5 PL/SQL procedure successfully completed.

3. sum of n natural number declare s number(4); a number(2); n number(2); begin n:=&num; s:=0; a:=1; while a<=n loop s:=s+a; a:=a+1; end loop; DBMS_OUTPUT.PUT_LINE('Sum of natural numbers'); DBMS_OUTPUT.PUT_LINE(s); end; old 6: n:=&num; new 6: n:=5; Sum of natural numbers 15 PL/SQL procedure successfully completed. 4.factorial declare n number(4); a number(4); b number(2); begin n:=&number; a:=1; for b in 1..n loop a:=a*b; end loop; DBMS_OUTPUT.PUT_LINE('The factorial is'); DBMS_OUTPUT.PUT_LINE(a); end; old 6: n:=&number; new 6: n:=6; The factorial is 720 PL/SQL procedure successfully completed. 5.leap year r not declare n number(4); begin n:=&num; if((n mod 4=0 and n mod 100!=0)or(n mod 400=0)) then DBMS_OUTPUT.PUT_LINE('The year is leap year'); else DBMS_OUTPUT.PUT_LINE('The year is not leap year'); end if; end;

old 4: n:=&num; new 4: n:=2005; The year is not leap year PL/SQL procedure successfully completed.

Ex. No. :6
1. declare cursor c1 is select empname,designation,dname from emp39,dept39 where emp39.empdno=dept39.dno; tname varchar2(10); tjob varchar2(10); tdepname varchar2(15); begin open c1; loop fetch c1 into tname,tjob,tdepname; exit when c1 %notfound; DBMS_OUTPUT.PUT_LINE(tname||' is a '||tjob||' Working in '||tdepname); end loop; close c1; end; Ramesh is a Manager Working in purchase Raja is a Clerk Working in purchase Mano is a Salesman Working in SALES Mano is a Accountant Working in accounts Raju is a Clerk Working in purchase Babu is a Clerk Working in PRODUCTION Ram is a Salesman Working in SALES Velu is a Programmer Working in PRODUCTION Ravi is a Accountant Working in accounts Balan is a Manager Working in PRODUCTION Mahesh is a Officer Working in purchase Kumar is a Analyst Working in software PL/SQL procedure successfully completed. 2. declare cursor c1 is select empname,designation,dloc from emp39,dept39 where emp39.empdno=dept39.dno; begin for erec in c1 loop DBMS_OUTPUT.PUT_LINE(erec.empname||' is a '||erec.designation||' in '||erec.dloc); end loop; end;

Ramesh is a Manager in blockd Raja is a Clerk in blockd Mano is a Salesman in blocka Mano is a Accountant in blockc Raju is a Clerk in blockd Babu is a Clerk in blockb Ram is a Salesman in blocka Velu is a Programmer in blockb Ravi is a Accountant in blockc Balan is a Manager in blockb Mahesh is a Officer in blockd Kumar is a Analyst in blocke PL/SQL procedure successfully completed. 3. declare cursor c1 is select * from emp39; erec emp39 %rowtype; begin for erec in c1 loop DBMS_OUTPUT.PUT_LINE(erec.empname||' is a '||erec.designation||' earning '||erec.salary); end loop; end; Ramesh is a Manager earning 2500.5 Raja is a Clerk earning 3000 Mano is a Salesman earning 3000 Mano is a Accountant earning 3000 Raju is a Clerk earning 7000 Babu is a Clerk earning 5000 Ram is a Salesman earning 3000.25 Velu is a Programmer earning 10000 Ravi is a Accountant earning 8000 Balan is a Manager earning 12000.75 Mahesh is a Officer earning 10000.5 Kumar is a Analyst earning 14500 PL/SQL procedure successfully completed. 4. declare type vtab is table of varchar2(10) index by binary_integer; tab vtab; j binary_integer:=0; cursor c1 is select empname from emp39; begin for i in c1 loop j:=j+1; tab(j):=i.empname; end loop; DBMS_OUTPUT.PUT_LINE('The total number of records is '||tab.count); DBMS_OUTPUT.PUT_LINE('The first record is '||tab(tab.first)); DBMS_OUTPUT.PUT_LINE('The last record is '||tab(tab.last));

DBMS_OUTPUT.PUT_LINE('The last but previous record is '||tab(tab.prior(tab.last))); DBMS_OUTPUT.PUT_LINE('The second record is '||tab(tab.next(tab.first))); tab.delete(1); DBMS_OUTPUT.PUT_LINE('The total number of records is '||tab.count); end; The total number of records is 12 The first record is Ramesh The last record is Kumar The last but previous record is Mahesh The second record is Raja The total number of records is 11 PL/SQL procedure successfully completed. 5. declare cursor c1 is select * from emp39 for update; cursor c2 is select * from emp39; begin for erec in c1 loop if erec.designation='Manager' then update emp39 set salary=salary+500 where current of c1; elsif erec.designation='Clerk' then update emp39 set salary=salary+200 where current of c1; end if; end loop; for emprec in c2 loop DBMS_OUTPUT.PUT_LINE(emprec.empname||' now earns '||emprec.salary); end loop; end; Ramesh now earns 3000.5 Raja now earns 3200 Mano now earns 3000 Mano now earns 3000 Raju now earns 7200 Babu now earns 5200 Ram now earns 3000.25 Velu now earns 10000 Ravi now earns 8000 Balan now earns 12500.75 Mahesh now earns 10000.5 Kumar now earns 14500 PL/SQL procedure successfully completed. 6. declare type rectab is record(code number(5),name varchar2(40)); type refcur is ref cursor return rectab recvar rectab; refcur_var refcur; opt number:=&n; begin if opt=1 then

open refcur_var for select empno,empname from emp39; elsif opt=2 then open refcur_var for select dno,dname from dept39; elsif opt=3 then open refcur_var for select itemcode,itemname from produ1; else DBMS_OUTPUT.PUT_LINE('Invalid Option'); end if; if opt=1 or opt=2 or opt=3 then loop fetch refcur_var into recvar; exit when refcur_var %notfound; DBMS_OUTPUT.PUT_LINE('Code is '||recvar.code||' name is '||recvar.name); end loop; close refcur_var; end if; end; Variable Value n þÿ
OK Cancel

old 6: opt number:=&n; new 6: opt number:=1; Code is 11 name is Ramesh Code is 12 name is Raja Code is 13 name is Mano Code is 14 name is Mano Code is 3 name is Raju Code is 4 name is Babu Code is 5 name is Ram Code is 6 name is Velu Code is 7 name is Ravi Code is 8 name is Balan Code is 9 name is Mahesh Code is 10 name is Kumar PL/SQL procedure successfully completed. Variable Value n þÿ
OK Cancel

old 6: opt number:=&n; new 6: opt number:=2; Code is 1 name is SALES Code is 2 name is purchase Code is 3 name is PRODUCTION Code is 4 name is marketing Code is 5 name is accounts Code is 6 name is software PL/SQL procedure successfully completed.

Variable Value n þÿ
OK Cancel

old 6: opt number:=&n; new 6: opt number:=3; Code is 1001 name is pen Code is 1002 name is pencil Code is 1003 name is ruler Code is 1004 name is eraser Code is 1005 name is sketch pen PL/SQL procedure successfully completed. Variable Value n þÿ
OK Cancel

old 6: opt number:=&n; new 6: opt number:=4; Invalid Option PL/SQL procedure successfully completed. 7. declare type tcur is ref cursor; tvar tcur; erec emp39 %rowtype; drec dept39 %rowtype; choice number:=&c; begin if choice=1 then open tvar for select * from emp39; loop fetch tvar into erec; exit when tvar %notfound; DBMS_OUTPUT.PUT_LINE(erec.empname); end loop; close tvar; elsif choice=2 then open tvar for select * from dept39; loop fetch tvar into drec; exit when tvar %notfound; DBMS_OUTPUT.PUT_LINE(drec.dno||' '||drec.dname); end loop; close tvar; else DBMS_OUTPUT.PUT_LINE('Invalid Choice'); end if;

end; Variable Value c þÿ
OK Cancel

old 6: choice number:=&c; new 6: choice number:=1; Ramesh Raja Mano Mano Raju Babu Ram Velu Ravi Balan Mahesh Kumar PL/SQL procedure successfully completed. Variable Value c þÿ
OK Cancel

old 6: choice number:=&c; new 6: choice number:=2; 1 SALES 2 purchase 3 PRODUCTION 4 marketing 5 accounts 6 software PL/SQL procedure successfully completed. Variable Value c þÿ
OK Cancel

old 6: choice number:=&c; new 6: choice number:=5; Invalid Choice PL/SQL procedure successfully completed.

Ex.No.:7
1. create or replace trigger deltemp before delete on dept39 for each row

begin delete from emp39 where(:old.dno=empdno); dbms_output.put_line('Data are deleted in employee table'); end; Trigger created. delete from dept39 where dno=6; Data are deleted in employee table 1 row deleted. 2. create or replace trigger instemp before insert or update on emp39 for each row begin if(:new.salary>2500) then raise_application_error(-20000,'Incorrect value'); else DBMS_OUTPUT.PUT_LINE('The employee table is updated'); endif; end; Trigger created. update emp39 set salary=4000 where empno=13 update emp39 set salary=4000 where empno=13 * ERROR at line 1: ORA-20000: Incorrect value ORA-06512: at "IT339.INSTEMP", line 3 ORA-04088: error during execution of trigger 'IT339.INSTEMP' update emp39 set salary=2000 where empno=13 The employee table is updated 1 row updated. 3. create or replace trigger inemp before insert on emp39 declare x number; begin select count(*) into x from emp39; DBMS_OUTPUT.PUT_LINE(x+1||'number of records available'); end; Trigger created. insert into emp39 values(15,'Latha','Programmer','30-AUG-92',2000,5,252100,'A'); 12number of records available 1 row created. 4. create or replace trigger chkdepno before update of empdno on emp39 for each row declare oldno number; begin

select dno into oldno from dept39 where dno=:new.empdno; exception when no_data_found then DBMS_OUTPUT.PUT_LINE('Depno not available'); end; Trigger created. update emp39 set empdno=6 where empno=16 Depno not available 1 row updated. 5. create or replace trigger chksaljob after insert on emp39 for each row declare raise_ex exception; begin if not(:new.designation='Manager' and (:new.salary>4000 and :new.salary<6000)) then raise raise_ex; end if; exception when raise_ex then DBMS_OUTPUT.PUT_LINE('Limit for manager is between 4000 and 6000'); end; Trigger created. 6. create or replace trigger chk_saljob after insert on emp39 for each row declare raise_ex exception; begin if not(:new.designation='Manager' and (:new.salary>4000 and :new.salary<6000)) then raise raise_ex; end if; exception when raise_ex then raise_application_error(-20000,'Limit for manager is between 4000 and 6000'); end; Trigger created. insert into emp39 values(17,'Saji','Manager','17-oct-89',3000,2,240608,'A'); insert into emp39 values(17,'Saji','Manager','17-oct89',3000,2,240608,'A') * ERROR at line 1: ORA-20000: Limit for manager is between 4000 and 6000 ORA-06512: at "IT339.CHK_SALJOB", line 10 ORA-04088: error during execution of trigger 'IT339.CHK_SALJOB' 7. alter trigger chk_saljob disable Trigger altered. alter trigger chk_saljob enable

Trigger altered. 8. drop trigger chk_saljob; Trigger dropped. 9. alter table emp39 disable all triggers Table altered. alter table emp39 enable all triggers Table altered.

Ex. No.:8
1. create or replace procedure itin(eno in number)as mname varchar2(20); begin select empname into mname from emp where empno=eno; DBMS_OUTPUT.PUT_LINE('The name is '||mname); end; Procedure created. exec itin(11); The name is Ramesh PL/SQL procedure successfully completed. 2. create or replace procedure disp_sal(eno in number,sal out number)as mname varchar2(20); begin select salary,empname into sal,mname from emp39 where empno=eno; DBMS_OUTPUT.PUT_LINE('The name is '||mname); end; Procedure created. var n number exec disp_sal(12,:n); print n; The name is Raja PL/SQL procedure successfully completed. N 3200

3. declare n number; mno number:=&x; begin disp_sal(mno,n); DBMS_OUTPUT.PUT_LINE('The salary is '||n); end; old 3: mno number:=&x; new 3: mno number:=7;

The name is Ravi The salary is 8000 PL/SQL procedure successfully completed. 4. create or replace procedure disp_emp(eno in out number,sal out number)as mname varchar2(10); begin select salary,empname into sal,mname from emp39 where empno=eno; DBMS_OUTPUT.PUT_LINE('The name is '||mname||' and salary is '||sal); end; Procedure created. declare name varchar2(20); var_x number:=&x; begin disp_emp(var_x,name); end; Variable Value x þÿ old 3: var_x number:=&x; new 3: var_x number:=11; The name is Ramesh and salary is 3000.5 PL/SQL procedure successfully completed. 5. create or replace procedure raise_salary(depno number,percentage number DEFAULT 0.5) is cursor emp_cur(dno number) is select salary from emp39 where empdno=depno for update of salary; empsal number(8); begin open emp_cur(depno); loop fetch emp_cur into empsal; exit when emp_cur % NOTFOUND; update emp39 set salary=empsal*((100+percentage)/100) where current of emp_cur; end loop; close emp_cur; commit; end raise_salary; Procedure created. exec raise_salary(3); PL/SQL procedure successfully completed. select * from emp39 where empdno=3; EMPNO EMPNAME DESIGNATIO 4 Babu Clerk 6 Velu Programmer 8 Balan Manager

DATE_OF_J SALARY EMPDNO PHONENO G 04-JAN-95 5226 3 E 24-DEC-02 10050 3 A 07-JUN-93 12563.51 3 A

6. create or replace function seldep(depno in number) return varchar2 is mname varchar2(20); begin select dname into mname from dept39 where dno=depno; return mname; end; Function created. select seldep(2) from dual; SELDEP(2) purchase 7. create or replace function get_dept_salary(dno number) return number is all_sal number; begin all_sal:=0; for empsal in(select salary from emp39 where empdno=dno and salary is NOT NULL) loop all_sal:=all_sal+empsal.salary; end loop; return all_sal; end get_dept_salary; Function created. select get_dept_salary(3) from dual; GET_DEPT_SALARY(3) 27839.51

8. create or replace function fns(n number) return number as
begin declare a number(2); b number(2); i number(2); s number(4); begin a:=-1; b:=1; s:=0; i:=0; loop s:=a+b; a:=b; b:=s; DBMS_OUTPUT.PUT_LINE(s); exit when i=n; i:=i+1; end loop;

return 1; end fibo; end; Function created. var n number; exec :n:=fns(5); 0 1 1 2 3 5 PL/SQL procedure successfully completed.

Ex. No.:9
1. connect system/manager; Connected. create user ras1 identified by afrase; User created. create user ras2 identified by azeera; User created. 2. grant create session to ras1; Grant succeeded. grant resource to ras1; Grant succeeded. grant resource to ras2; Grant succeeded. grant create session to ras2; Grant succeeded. 3. connect ras1/afrase; Connected. create table student(name varchar2(14),age number(2)); Table created. desc student; Name Null? NAME AGE insert into student values('Afrase',18); insert into student values('Azeera',19);

Type VARCHAR2(14) NUMBER(2)

1 row created. 1 row created. 4. grant select on student to ras2; Grant succeeded. connect ras2/azeera Connected. select * from ras1.student; NAME Afrase Azeera select * from student; select * from student * ERROR at line 1: ORA-00942: table or view does not exist 5. revoke select on student from ras2 revoke select on student from ras2 * ERROR at line 1: ORA-00942: table or view does not exist connect ras1/afrase Connected. revoke select on student from ras2 Revoke succeeded. connect ras2/azeera Connected. select * from ras1.student select * from ras1.student * ERROR at line 1: ORA-00942: table or view does not exist connect it339/it339 Connected. grant all on dept39 to ras1 with grant option Grant succeeded. connect ras1/afrase Connected. select * from it339.dept39 DNO 1 SALES 2 purchase AGE 18 19

DNAME blocka blockd

DLOC

3 PRODUCTION 4 marketing 5 accounts

blockb blocka blockc

grant all on it339.dept39 to ras2 Grant succeeded. connect ras2/azeera Connected. grant all on it339.dept39 to ras1 grant all on it339.dept39 to ras1 * ERROR at line 1: ORA-01929: no privileges to GRANT connect ras1/afrase Connected. revoke all on it339.dept39 from ras2; Revoke succeeded. revoke all on student from ras2; Revoke succeeded. connect it339/it339 Connected. revoke all on dept39 from ras1 Revoke succeeded. revoke all on dept39 from ras2 Revoke succeeded.

Sign up to vote on this title