Вы находитесь на странице: 1из 21

1)//fetching one column declare no employees.first_name%type; sal employees.

salary%type; begin no:=:no; select salary into sal from employees where employee_id=no; dbms_output.put_line(sal); end; 2)//fetching more one column declare no employees.employee_id%type; sal employees.salary%type; fn employees.first_name%type; begin no:=:no; select salary,first_name into sal,fn from employees where employee_id=no; dbms_output.put_line('Employee_id '||fn||' salary '||sal); end; 3)//insert into table with user-defined inputs declare emp employees.employee_id%type; name employees.Last_name%type; email employees.email%type; hd employees.hire_date%type; jid employees.job_id%type; begin emp:=:emp; name:=:name; email:=:email; hd:=:hiredate; jid:=:job_id; insert into employees(employee_id,Last_name,email,hire_date,job_id) values(emp,name,email,hd,jid); dbms_output.put_line('succesful'); end; 4)//calculating percentage with user-defined inputs declare emp employees.employee_id%type; per number; cal number; sal employees.salary%type; begin emp:=:emp; per:=:percentage; cal:=floor(per/100); select salary into sal from employees where employee_id=emp; dbms_output.put_line('original salary'||sal); update employees set salary=sal+per where employee_id=emp; dbms_output.put_line('successful'); select salary into sal from employees where employee_id=emp; dbms_output.put_line('Updated salary'||sal); end;

5)//deleting user-defined data declare emp employees.employee_id%type; begin emp:=:EMPLOYEE_ID; delete from employees where employee_id=emp; dbms_output.put_line('succesfully deleted'); end; 6)//max,min,avg declare dept employees.department_id%type; mn number; mx number; av number; begin dept:=:departmentid; select avg(salary) into av from employees where department_id=dept; select min(salary) into mn from employees where department_id=dept; select max(salary) into mx from employees where department_id=dept; dbms_output.put_line('Average '||av); dbms_output.put_line('Minimum '||mn); dbms_output.put_line('Maximum '||mx); end; 7)//explicit curcor declare cursor c1 is select * from employees where department_id=90; mrec employees%rowtype; begin open c1; loop fetch c1 into mrec; exit when c1%notfound; dbms_output.put_line(mrec.first_name); end loop; close c1; end; 8)//Parameterised cursor; declare cursor c1(dept number) is select * from employees where department_id=dept; mrec employees%rowtype; no number; begin no:=:depart_id; open c1(no); loop fetch c1 into mrec; exit when c1%notfound; dbms_output.put_line(mrec.first_name); end loop; close c1; end;

9)//Using Cursor for loops; declare cursor c1(dept number) is select * from employees where department_id=dept; mrec employees%rowtype; no number; begin no:=:depart_id; for mrec in c1(no) loop dbms_output.put_line(mrec.first_name); end loop; end; 10)//For Update cursor; declare cursor c1(dept number) is select * from employees where department_id=dept for update of salary nowait; mrec employees%rowtype; no number; begin no:=:depart_id; for mrec in c1(no) loop dbms_output.put_line('firstname '||mrec.first_name||' and salary '||mrec.salary); update employees set salary=mrec.salary+500 where current of c1; end loop; end;

11)//Weak_cursor variable type(user defined dynamic cursors) declare TYPE emp_cur IS REF CURSOR; v_weak_emp emp_cur; v_emp_rec EMPLOYEES%ROWTYPE; v_dep_rec DEPARTMENTS%ROWTYPE; BEGIN OPEN v_weak_emp FOR SELECT * FROM employees; LOOP FETCH v_weak_emp INTO v_emp_rec; EXIT WHEN v_weak_emp%NOTFOUND; DBMS_OUTPUT.PUT_LINE('Firstname- '||v_emp_rec.first_name); END LOOP; close v_weak_emp; OPEN v_weak_emp FOR SELECT * FROM departments; LOOP FETCH v_weak_emp INTO v_dep_rec; EXIT WHEN v_weak_emp%NOTFOUND; DBMS_OUTPUT.PUT_LINE('Departments- '||v_dep_rec.Department_name); END LOOP; close v_weak_emp; END; select * from employees;

select * from departments; 12)Exception(when no_data_found) declare v_emp employees%ROWTYPE; begin select * into v_emp from employees where employee_id=768; dbms_output.put_line('empno'||v_emp.employee_id); EXCEPTION WHEN NO_DATA_FOUND THEN dbms_output.put_line('employees does not exist'); end; 13)//pragma_exception declare e_emp_exist exception; pragma exception_init(e_emp_exist,-2292); v_deptno departments.department_id%type:=:deptno; begin delete from departments where department_id=v_deptno; commit; exception when e_emp_exist then dbms_output.put_line('can not delete dept '); end; 14)//user-defined exception declare --declaring user-defined exception e_emp_exist exception; begin update employees set salary=salary+15000 where employee_id=1001; if sql%notfound then --raising user-defined exception RAISE e_emp_exist; end if; commit; exception when e_emp_exist then dbms_output.put_line('invalid employee_id'); end; 15)//Is age > 80,raise user-defined exception declare --declaring user-defined exception e_emp_exist exception; emp employees.employee_id%type; age number; begin emp:=:employee_id; age:=:age; if (age>80) then RAISE e_emp_exist; end if;

select first_name into emp from employees where employee_id=emp; dbms_output.put_line('firstname '||emp||' and age is '||age); commit; exception when e_emp_exist then dbms_output.put_line('invalid age'); end; 16)//Is commission_pct > 0.9,raise user-defined exception declare e_emp_exist exception; cm employees.commission_pct%type; num number; pragma exception_init(e_emp_exist,-01438); begin num:=:number; if (num > 0.9) then RAISE e_emp_exist; end if; update employees set commission_pct=num where employee_id=101; select commission_pct into cm from employees where employee_id=101; dbms_output.put_line('the revised commission pact is '||cm); commit; exception when e_emp_exist then dbms_output.put_line('invalid number'); end; 17)//To print too_many_rows and no_data_found exception declare emp employees.first_name%type; sal employees.salary%type; begin emp:=:employee_name; select salary into sal from employees where first_name=emp; dbms_output.put_line('Salary '||sal); commit; EXCEPTION WHEN NO_DATA_FOUND THEN dbms_output.put_line('employees does not exist'); WHEN TOO_MANY_ROWS THEN dbms_output.put_line('Too many employees with the same name'); end; 18)With cursor throw userdefined as well as system defined exception//userdefined will run first. declare NO_DATA_FOUND1 exception; cursor c1 is select * from employees where first_name='Johnd'; mrec employees%rowtype; begin open c1; loop fetch c1 into mrec; if (c1%notfound) then

raise NO_DATA_FOUND1; end if; dbms_output.put_line('First_name '||mrec.first_name); dbms_output.put_line('Salary '||mrec.salary); end loop; close c1; commit; EXCEPTION WHEN NO_DATA_FOUND1 THEN dbms_output.put_line('1employees does not exist'); WHEN NO_DATA_FOUND THEN dbms_output.put_line('2employees does not exist'); end; 19)Without cursor throw userdefined as well as system defined exception//systemdefined will run first. declare NO_DATA_FOUND1 exception; sal employees.salary%type; emp employees.first_name%type; begin emp:=:employee_name; select salary into sal from employees where first_name=emp; dbms_output.put_line('Salary '||sal); if (sql%notfound) then raise NO_DATA_FOUND1; end if; commit; EXCEPTION WHEN NO_DATA_FOUND1 THEN dbms_output.put_line('1employees does not exist'); WHEN NO_DATA_FOUND THEN dbms_output.put_line('2employees does not exist'); end; 20)//nesting of two precedures: declare x number:=10; begin dbms_output.put_line(x); declare x number:=20; begin dbms_output.put_line(x); end; end; output:-10 20 21)//nesting but still printing the original value:define <<any_name>> before declare statement: <<a>> declare x number:=10; begin dbms_output.put_line('x= '||x);

declare x number:=20; begin dbms_output.put_line('x= '||a.x); end; end; o/p=10 10 22)//nesting but still printing the original value:b.x wont get execute because it has lost its scope; <<a>> declare x number:=10; begin dbms_output.put_line('x= '||x); <<b>>declare x number:=20; begin dbms_output.put_line('x= '||a.x); end; dbms_output.put_line('x= '||b.x); end; o/pPLS-00219: label 'B' reference is out of scope

23.i)//declaring exception in the inner block..workin declare x number:=10; begin dbms_output.put_line('x= '||x); declare x number:=20; begin dbms_output.put_line('x= '||x/0); exception when zero_divide then dbms_output.put_line('divide by zero expression'); end; end; 23.ii)//declaring exception only on the outside block...still working! declare x number:=10; begin dbms_output.put_line('x= '||x); declare x number:=20; begin dbms_output.put_line('x= '||x/0); end; exception when zero_divide then dbms_output.put_line('divide by zero expression'); end;

23.iii)//declaring exception in both the blocks;working....but the inner msg is only displayed...as the oracle searches for the immediate exception defined....blocks wise... declare x number:=10; begin dbms_output.put_line('x= '||x); declare x number:=20; begin dbms_output.put_line('x= '||x/0); exception when zero_divide then dbms_output.put_line('divide by zero expression'); end; exception when zero_divide then dbms_output.put_line('divide by zeroooo expression'); end; 24)for incrementin salary the values by using procedure and then passing values from user to dat procedure; create or replace procedure pr_incre(sal employees.salary%type,emp employees.employee_id%type) IS BEGIN update employees set salary=salary+sal where employee_id=emp; dbms_output.put_line('salary '||sal); end; execute pr_incre(15000,102); declare sal employees.salary%type; id employees.employee_id%type; begin sal:=:sal; id:=:id; pr_incre(sal,id); end; o/p=salary 14000 Statement processed. 25)for inserting the values by using procedure and then using then passing values from user to dat procedure; create or replace procedure pr_insert(id student.id%type,nm student.name%type,sal student.salary%type) IS BEGIN insert into student values(id,nm,sal); dbms_output.put_line('values inserted'); end; drop procedure pr_insert//to drop a procedure.

declare id student.id%type; nm student.name%type; sal student.salary%type; begin id:=:id; nm:=:name; sal:=:sal; pr_insert(id,nm,sal); end; o/p=values inserted Statement processed. 26)Using OUT within procedure: create or replace procedure psal(empid employees.employee_id%type,sal out employees.salary%type) is begin select salary into sal from employees where employee_id=empid; end; declare emp employees.employee_id%type; sal employees.salary%type; begin emp:=:Employee_ID; psal(emp,sal); dbms_output.put_line('salary='||sal); end; 27)//IN OUT procedure example create or replace procedure p_sal_inout(empid employees.employee_id%type,sal in out employees.salary %type) is s1 employees.salary%type; begin select salary into s1 from employees where employee_id=empid; sal:=s1+sal; end; declare emp employees.employee_id%type; sal employees.salary%type; begin emp:=:Employee_ID; sal:=:salary; p_sal_inout(emp,sal); dbms_output.put_line('salary='||sal); end; 28)create procedure to find department which will accept emp_id and print its department_name;

create or replace procedure p_emp(empid employees.employee_id%type,depname in out departments.department_name%type) is begin select d.department_name into depname from employees e,departments d where e.employee_id=empid and e.department_id=d.department_id; end; declare emp employees.employee_id%type; depname departments.department_name%type; begin emp:=:Employee_ID; p_emp(emp,depname); dbms_output.put_line('department='||depname); end; 29)write a procedure incrs_sal which will increase the salary of employees on following basis If emp works for more than 10 years..raise his salary by 10%; between 5-10 raise by 5%; if above 2 years raise by 3%; else 2%; create or replace procedure p_j1(empid employees.employee_id%type,sal in out number) is s1 employees.salary%type; h1 employees.hire_date%type; m number(8); begin select hire_date,salary into h1,s1 from employees where employee_id=empid; m:=round(months_between(sysdate,h1)/12,2); if m > 10 then sal:=s1+(0.1*s1); elsif m >= 5 and m < 10 then sal:=s1+(0.05*s1); elsif m > 3 then sal:=s1+(0.03*s1); else sal:=s1+(0.02*s1); end if; end; select sysdate from dual; select round(months_between(sysdate,hire_date)/12) from employees; declare emp employees.employee_id%type; sal employees.salary%type; begin emp:=:Employee_ID; p_j1(emp,sal); dbms_output.put_line('salary='||sal); end;

30) create or replace procedure multi_para(p1 IN number,p2 IN OUT number,p3 IN OUT NOCOPY number) is begin dbms_output.put_line('started multiple param'); dbms_output.put_line('p1=' ||p1); p2:=20; dbms_output.put_line('p2:=20 then p1=' ||p1); p3:=30; dbms_output.put_line('p3:=30 then p1=' ||p1); dbms_output.put_line('Finished multiple param'); end; declare y number; begin y:=10; dbms_output.put_line('start y '||y); multi_para(y,y,y); dbms_output.put_line('end y '||y); end; 31)//Calculating simple interest create or replace function sim_int(p number,n number,r number) return number is res number; begin res:=round(p*n*r/100); return res; end; declare result number; p number; n number; r number; begin p:=:Principal_amount; n:=:Num_of_years; r:=:Rate_Of_interest; result:=sim_int(p,n,r); dbms_output.put_line('Simple Interest='||result); end; 32)//accept emp_id and print for how many years is the emp working in the company. create or replace function f_years(emp number) return number is hd employees.hire_date%type; calc number; begin select hire_date into hd from employees where employee_id=emp;

calc:=round(months_between(sysdate,hd)/12); return calc; end; declare emp_id number; result number; begin emp_id:=:employee_id; result:=f_years(emp_id); dbms_output.put_line('Employee_ID='||emp_id||' has worked for '||result||' years'); end; input:employee_id=102 o/p= Employee_ID=102 has worked for 19 years 33)Exception handling when no data found(system defined exception) create or replace function f_years(emp number) return number is hd employees.hire_date%type; calc number; begin select hire_date into hd from employees where employee_id=emp; calc:=round(months_between(sysdate,hd)/12); return calc; exception when no_data_found then dbms_output.put_line('nooooo data found'); return 0; end;

declare emp_id number; result number; begin emp_id:=:employee_id; result:=f_years(emp_id); dbms_output.put_line('Employee_ID='||emp_id||' has worked for '||result||' years'); end; 34) create or replace package maths_fun as function add1(no1 number,no2 number) return number; function mult1(no1 number,no2 number) return number; function div1(no1 number,no2 number) return number; function sub1(no1 number,no2 number) return number; end maths_fun; create or replace package body maths_fun is function add1(no1 number,no2 number) return number is

no3 number; begin no3:=no1+no2; return no3; end add1; function mult1(no1 number,no2 number) return number is no3 number; begin no3:=no1*no2; return no3; end mult1; function sub1(no1 number,no2 number) return number is no3 number; begin no3:=no1-no2; return no3; end sub1; function div1(no1 number,no2 number) return number is no3 number; begin no3:=no1/no2; return no3; end div1; end maths_fun; select maths_fun.mult1(23,2) as mul from dual; o/p= mul 46 TRIGGERS 35)//insert into x_student after deleting it from student; TABLES: student=id,name; x_student=id,name; create or replace trigger tr_emp before delete on student for each row begin insert into x_student values(:old.id,:old.name); //column names from student end; o/p= earlier record: select * from student; ID 101 102 NAME rahul vidya

103

nikki

delete from student where id=103; 1 row deleted; select * from x_student; ID 103 NAME nikki

36)//update a table...before inserting w new record..insert the old one into new table; create or replace trigger tr_emp_up before update on student for each row begin insert into x_student values(:old.id,:old.name); end; update student set salary=salary+15000 where name='rahul' select * from x_student;

37)//giving access to user to delete only particular set of records. create or replace trigger tr_del before delete on student for each row begin if :old.id<101 then raise_application_error(-20100,'cant delete this record'); end if; end; o/p= delete from student where id=100; ORA-20100: cant delete this record ORA-06512: at "HR.TR_DEL", line 3 38)//for enabling and disabling trigggers from a table; alter trigger trigger_name disable/enable;//delete only a specific trigger; alter table student disable/enable all triggers;//delete all trigger on particular; select * from user_triggers;//select every trigger definations. 39)//AFTER DELETE....after deleting insert into del_date table. create table del_date ( user_name varchar(40), del_date varchar(40) )

create or replace trigger tri_aftr_del after delete on student begin insert into del_date values(user,sysdate); end; delete from student where id=103; select * from del_date o/p= USER_NAME DEL_DATE ANONYMOUS 14-JAN-12 40) alter table employees add status varchar(40); create or replace package pk_status27 is procedure pr_status; function fn_status(emp number) return date; end pk_status27; create or replace package body pk_status27 is procedure pr_status is begin update employees set status='confirmed' where round(months_between(sysdate,hire_date)/12)>1 ; update employees set status='Not confirmed' where round(months_between(sysdate,hire_date)/12)<=1 ; end pr_status; function fn_status(emp number) return date is hd employees.hire_date%type; calc date; begin select hire_date into hd from employees where employee_id=emp; calc:=add_months(hd,12); return calc; end fn_status; end pk_status27; declare emp number; begin emp:=:employee_id; pk_status27.pr_status(); dbms_output.put_line('Confirmation date='||pk_status27.fn_status(emp)); end; o/p=

for running function: earlier record=21-SEP-89 Confirmation date=21-SEP-90

41)//in which department max number of employees are working; using inline view: select department_id from (select department_id,count(employee_id) emps from employees group by department_id) where emps=(select max(emps) from (select department_id, count(employee_id) emps from employees group by department_id)); using view: create view vw_dep_emp as select department_id,count(employee_id) alias from employees group by department_id; select * from vw_dep_emp where alias=(select max(alias) from vw_dep_emp); o/p= DEPARTMENT_ID 50 ALIAS 45

42)//create trigger which cannot be deleted on which DML statements cannot be performed on sundays: create or replace trigger tr_sun before delete or insert or update on employees for each row begin if to_char(sysdate,'d')=1 then raise_application_error(-20101,'cant delete or update or insert this record on sunday'); end if; end; update employees set salary=salary+10000 where employee_id=108; o/p=//after changing the system date to sunday: ORA-20101: cant delete or update or insert this record on sunday 43)//create 3 tables...check the when user enters anythng in order table it shud place an order only if (certain condition) into purchase table refering to product table. create table product_info ( prod_id varchar(20) constraint p_id primary key, prod_name varchar(40), min_qty number, qty_stock number ) create table order_info ( o_no varchar(40) primary key, order_date date, prod_id varchar(20) references product_info(prod_id), qty number

) create table purchase ( prod_id varchar(20) references product_info(prod_id), o_no varchar(40) references order_info(o_no), qty number, purchase_date date ) insert into product_info values('101','pens',60,70); insert into product_info values('102','pencils',80,90); insert into product_info values('103','Rubber',70,80); insert into product_info values('104','Laptops',50,60); o/p= PROD_ID PROD_NAME MIN_QTY QTY_STOCK 104 Laptops 50 60 103 Rubber 70 80 102 pencils 80 90 101 pens 60 70 create or replace trigger tr_ord_pur after insert on order_info for each row declare mn_qt number; qt_st number; calc number; begin select min_qty,qty_stock into mn_qt,qt_st from product_info where prod_id=:new.prod_id; if (:new.qty > (qt_st-mn_qt)) then calc:=:new.qty-(qt_st-mn_qt); insert into purchase values(:new.prod_id,:new.o_no,calc,:new.order_date); else raise_application_error(-20102,'you cannot place an order'); end if; end; insert into order_info values('4','18-jan-12','102',100); insert into order_info values('3','17-jan-12','103',100); insert into order_info values('2','17-jan-12','104',100); select * from purchase; o/p= PROD_ID O_NO 104 2 103 3 102 4

QTY 90 90 90

PURCHASE_DATE 17-JAN-12 17-JAN-12 18-JAN-12

44)//creating a view to insert records into a 2 base tables..will show error as view cannot insert simultaneously.. therefore creating a trigger when the view is executed..so that records are updated at both the places: create view emp_dept as

select employee_id,last_name,hire_date,job_id,email,e.department_id,department_name from employees e,departments d where e.department_id=d.department_id insert into emp_dept values (209,'amit',sysdate,'ad_vp','ss@h.com',200,'testing') o/p= ORA-01776: cannot modify more than one base table through a join view Therefore we use INSTEAD OF trigger: create or replace trigger view_insert instead of insert on emp_dept for each row begin insert into departments(department_id,department_name) values(:new.department_id,:new.department_name); insert into employees(employee_id,last_name,hire_date,email,job_id,department_id) values(:new.employee_id,:new.last_name,:new.hire_date,:new.email,:new.job_id,:new.department_id); end; insert into emp_dept values (209,'Amit',sysdate,'AD_VP','ss@h.com',205,'testing') o/p=Now the values will be inserted in both the tables. 45)using WHENs create or replace trigger tr_when before update of salary on employees for each row when (old.salary > new.salary) begin raise_application_error(-20010,'cannot reduce salary'); end; update employees set salary=salary-10000 where employee_id=102 o/p=ORA-20010: cannot reduce salary COLLECTIONS 46)//create 3 addresses of a employee create or replace type address_va AS VARRAY(3) of varchar2(50); //using keyword "type " and using it as a datatype; create table borrower ( borrower_id varchar2(10), borrower_name varchar2(50), borrower_add varchar2(100), addresses address_va ) insert into borrower values(1001,'James','D2-14',address_va('address1','address2','address3')); select * from borrower

o/p= borrower_id 10001

borrower_name James

borrower_add D2-14

addresses address_va('address1','address2','address3')

47)//create a table and accept 5 marks of a single employee create or replace type marks_type as varray(5) of number(3); create table stud ( name varchar(40), marks marks_type, city varchar(40) ) insert into stud values('rahul',marks_type(40,45,42,41,49),'mumbai'); select * from stud; o/p= same as above but for marks; 48)//for fetching records of a collection type.(on e=borrower table) declare ads address_va; //use this datatype...because we have declared it as a collection datatype. i number; begin select addresses into ads from borrower where borrower_id=1001; for i in 1..ads.count //count is a method to fetch values from the collection; loop dbms_output.put_line(ads(i)); end loop; end; output= address1 address2 address3 Statement processed.

49)//NESTING OF TABLES create or replace type depend_ty as OBJECT ( dependent_name varchar(40), relationship varchar(40), birth_date DATE ); create or replace type dependent_nt As TABLE Of depend_ty; create table borrow ( borrow_id varchar2(20),

borrow_name varchar2(20), borrow_add varchar2(20), dependence dependent_nt ) NESTED TABLE dependence STORE AS dependent_nested; insert into borrow values('101','rahul','mumbai',dependent_nt(depend_ty('jacob','father','21-sep1990'),depend_ty('nikki','Friend','17-sep-1990'),depend_ty('nisha','friend','27-jan-1990'))); o/p= 1 row inserted; 50)// to view the nested columns record: statement=select borrow_name,N.dependent_name,N.relationship,N.birth_date FROM borrow,TABLE(borrow.dependence) N o/p= borrow_name dependent_name relantionship birth_date rahul jacob father 21-sep-1990 rahul nikki friend 17-sep-1990 rahul nisha friend 27-jan-1990

statement=SET DESCRIBE DEPTH 2 DESC borrow //write both the statements seperately o/p= borrow_id borrow_name borrow_add dependence dependent_name relationship birth_date varchar2(20) varchar2(20) varchar2(20) dependent_nt varchar(40) varchar(40) DATE

51)//to create trigger whenever users login; create table details ( username varchar(20), logon_date date ) create or replace trigger logon_tr after logon on database //logon and datadase are the keywords begin insert into details values(user,sysdate); end; o/p=log in to system/redhat....create the table and trigger...logout and connect through command prompt.. select * from details;

Username SYSTEM

login_date 17-JAN-12

52)//creating user and using GRANT & REVOKE log in with system/redhat: create user username_name identified by password_name; o/p=user created grant connect to rahul; o/p=grant succeded log in with rahul/rahul: o/p=connected log in with system/redhat: grant all privileges to rahul; seperated by a comne.. o/p=grant succeded log in with rahul/rahul: create table.......................etc.. o/p=created! -revoke create table,create view from rahul //you can only revoke those privileges which u hav given to the user...or simply if u have granted all the privileges then u can use: -revoke all privileges from rahul //grant all rights to rahul,if specfic then put grant create table,create view

Вам также может понравиться