Sunteți pe pagina 1din 9

1) write a pl/sql trigger to trace empno,ename,sal,deptno after remove the emp(f rom emp table) details to store this

data in trace table. create table trace as select empno ,ename ,sal ,deptno from emp having 'oracle'='sql server'; create or replacetrigger trg_trace after delete on emp for each row begin insert into trace values (:old.empno,:old.ename,:old.sal,:old.deptno); end; Note: delete from emp where deptno=20; select *from trace; Rollback; select *from trace; select *from emp; 2) create pl/sql trigger program after delete the emps from emptable trace the user,date of joining, empno,and sal the information will display in del_trace. create table del_trace (userid varchar2(20), doj timestamp, ecode number(4), sal number(10)); create or replace trigger trace_trg after delete on emp for each row begin insert into del_trace values(user,sysdate,:old.empno,:old.sal); end; Note: delete from emp where deptno=10; select *from del_trace; 3) write a pl/sql trigger to insert the empno,enmae,sal,deptno in del_trace afte r remove the records from emp table the emp's which is having sal above 1500. create table del_trace ( empno number(10),ename varchar2(20),sal number(7,2),deptno number(10)); create or replace trigger trace_trg

after delete on emp for eachrow when(old.sal>1500) begin insert into del_trace values(:old.empno,:old.ename,:old.sal,:old.deptno); end; Note: delete from emp where deptno=30; 2 records insert into del_trace bcz(one record above 1500). 5)write a trigger to insert the data into a bonus table after insert the record into the emp table and save the transaction only bonus table. create table bonus as select ename,job,sal,comm from emp where 'krish'='ravan'; create or replace trigger add_bonus after insert on emp for each row declare pragma autonomous_transaction; begin insert into bonus values(:new.ename,:new.job,:new.sal,:new.comm); commit; end; Note: insert into emp(empno,ename,job,sal,comm)values (1003,'prakash','softwarew','20000.900); the above row insert into emp table and bonus table select *from bonus; note:Bonus table Records save perminently. emp table records will be rollback. write a pl/sql Trigger i)week ends ii)Before 9 A.M after 7 p.m iii) public Holidays no transactions create table h_tab ( hday date ); insert into h_tab values('01-jan-11'); insert into h_tab values('14-jan-11'); ('26-jan-11'); ('15-aug-11') ('25-Dec-11');

commit; Note: select count(hday) from h_tab where hday=to_date (sysdate,'dd-mon-yy'); select *from h_tab; create or replace Trigger trg_dth before insert or update or delete on emp declare cnt number; begin select count(*)into cnt from h_tab where hday=to_date(sysdate,'dd-mm-yy'); if to_char(sysdate,'DY') in ('sat','sun') then Raise_application_error(-23456,'no transaction in week_end'); elsif cnt>0 then raise_application_error(-20457,'notransaction in hday'); elsif to_char(sysdate,'hh24')not between 9 and 19 then Raise_application_error (-20456,'notransaction in this time'); end if; end;

1) write a pl/sql trigger to trace empno,ename,sal,deptno after remove the emp(f rom emp table) details to store this data in trace table. create table trace as select empno ,ename ,sal ,deptno from emp having 'oracle'='sql server'; create or replacetrigger trg_trace after delete on emp for each row begin insert into trace values (:old.empno,:old.ename,:old.sal,:old.deptno); end; Note: delete from emp where deptno=20; select *from trace; Rollback; select *from trace; select *from emp; 2) create pl/sql trigger program after delete the emps from emptable trace the user,date of joining, empno,and sal the information will display in del_trace.

create table del_trace (userid varchar2(20), doj timestamp, ecode number(4), sal number(10)); create or replace trigger trace_trg after delete on emp for each row begin insert into del_trace values(user,sysdate,:old.empno,:old.sal); end; Note: delete from emp where deptno=10; select *from del_trace; 3) write a pl/sql trigger to insert the empno,enmae,sal,deptno in del_trace afte r remove the records from emp table the emp's which is having sal above 1500. create table del_trace ( empno number(10),ename varchar2(20),sal number(7,2),deptno number(10)); create or replace trigger trace_trg after delete on emp for eachrow when(old.sal>1500) begin insert into del_trace values(:old.empno,:old.ename,:old.sal,:old.deptno); end; Note: delete from emp where deptno=30; 2 records insert into del_trace bcz(one record above 1500). 5)write a trigger to insert the data into a bonus table after insert the record into the emp table and save the transaction only bonus table. create table bonus as select ename,job,sal,comm from emp where 'krish'='ravan'; create or replace trigger add_bonus after insert on emp for each row declare pragma autonomous_transaction; begin insert into bonus values(:new.ename,:new.job,:new.sal,:new.comm); commit; end;

Note: insert into emp(empno,ename,job,sal,comm)values (1003,'prakash','softwarew','20000.900); the above row insert into emp table and bonus table select *from bonus; note:Bonus table Records save perminently. emp table records will be rollback. write a pl/sql Trigger i)week ends ii)Before 9 A.M after 7 p.m iii) public Holidays no transactions create table h_tab ( hday date ); insert into h_tab values('01-jan-11'); insert into h_tab values('14-jan-11'); ('26-jan-11'); ('15-aug-11') ('25-Dec-11'); commit; Note: select count(hday) from h_tab where hday=to_date (sysdate,'dd-mon-yy'); select *from h_tab; create or replace Trigger trg_dth before insert or update or delete on emp declare cnt number; begin select count(*)into cnt from h_tab where hday=to_date(sysdate,'dd-mm-yy'); if to_char(sysdate,'DY') in ('sat','sun') then Raise_application_error(-23456,'no transaction in week_end'); elsif cnt>0 then raise_application_error(-20457,'notransaction in hday'); elsif to_char(sysdate,'hh24')not between 9 and 19 then Raise_application_error (-20456,'notransaction in this time'); end if; end;

write a pl/sql function to calculate the compound intrest. create or replace function comintr (p in number,n in number,r in number)

return number is ci number; begin ci:=p*power((1+r/100),n); return(ci); end comintr; select comintr(100,40,60) "commintr" from dual; var ci number exec :ci:=comintr(100,23,4) print :ci call this function in a different pl/sql function. begin display('the comm intr is:'||round((comintr(1000,12,2))); end; write a function accept the deptno as input and display the no of emps through t he return by using this function. display the deptno,and no.of employees the dept which is having morethan 3 emplo yees. create or replace function noe(pdno in number) return number is vnoe number; begin (select count(empno)into vnoe from emp where deptno=pdno;) return (vnoe); end noe; select unique deptno,noe(deptno) "noe"from emp where noe(deptno)>3; write a pl/sql procedure for inserting for records in emp table and commit the t ransaction. Create or replace procedure add_emp Is Begin Insert into emp(empno,ename,sal,deptno) Values(1001, james ,10000,44); Commit; End add_emp; Create or replace procedure add_dept Is Begin Insert into dept values(55, sss , chicago ); Add_emp; End add_dept; Note: emp table commit effected to dept table also.

Pragma autonomous_transaction: Create or replace procedure add_empo As Pragma autonomous_transaction; Begin Insert into emp(empno,ename,sal,deptno) values(1001, efford ,1000,55); Commit; End add_empo; Create or replace procedure add_depto As Begin Insert into dept values(44, jjj , chicago ); Add_empo; End add_depto; Note; commit is effected for only emp transaction Recompile an existing procedure: Alter procedure <proc-name>compile;

Synonyms on procedure: Create public synonym ad for add_dept Grant execute on ad to public Conn c/c Exec ad Conn scott/tiger Exec ad

Create or replace package add_mul As Procedure add_num(a in number,b in number); Function mul_num(x in number,y in number) Return number; Result number; End add_mul; Create or replace packagebody add_mul As Procedure add_num(a in number,b in number) Is Begin Result:=a+b; Dbms_output.put_line( the sum of a and b is: ||Result); End add_num; Function mul_num(x in number,y in number)

Return number Is Begin Result:=x*y; Return(result); End mul_num; End add_mul; To invoke the function in Select stmt. Select add_mul.mul_num(10,5) from dual; Select add_num.mul_num(20,300)from dual; Exec add_mul.add_num(100,200);

Develop a pl/sql package to display the emp gross salary emp experience in year By using two functions. Create or replace package exp_gross Is Function gross(psal in number) Retun number; Function exp(pdoj in date) Return number; End exp_gross; Create or replace packagebody exp_gross Is Function gross(psal in number) Return number Is Begin Return(round(psal+psal*0.35+psal*0.45-psal*0.15)); End gross; Function exp(pdoj in date) Return number; Is Begin Return(round(months_between(sysdate,pdoj)/12)); End exp; End exp_gross; Package Supports function overloading: Create or replace package pack_oload Is Function fun_load(a in number, b in number) Return number; Function fun_load(x in varchar2,y in varchar2) Return varchar2; End pack_oload;

Create or replace package pack_oload Is Function fun_load( a in number, b in number) Return number; Is Begin Return(a*b); End fun_load; Function fun_load(x in varchar2,y in varchar2) Return varchar2 Is Begin Return(x|| ||y); End fun_load; End pack_load;

S-ar putea să vă placă și