Programs Based on Exception Handling and Trigger.


                                                       PRACTICAL-11

Aim :-  Programs Based on Exception Handling and Trigger.

1. Table: STUDENT (sr_no, name, address). Write a pl/sql code to retrieve the data from                student table whose sr.no is 02ce33. if   data not found then show the message “data not                found “  (Use EXCEPTION in pl/sql code).
Ans :-

declare
no int;
nm varchar2(20);
begin
no:=&no;
select NAME into nm from student where ROLLNO=no;
dbms_output.put_line('Name of student is '||nm);
exception
when NO_DATA_FOUND then
dbms_output.put_line('No data found!!!');
end;
/

2. Give Example of “TOO_MANY_ROWS” Exception.
    Give Example of Unnamed system Exception.(Give Example of Primary key constraint  violation)
Ans:-

declare

nm varchar2(20);
begin
select name into nm from student;
dbms_output.put_line('Name of student is '||nm);
 exception
when TOO_MANY_ROWS then
dbms_output.put_line('Too many rows!!!');
end;
/

declare
primary_ky exception;
pragma Exception_init(primary_ky,-0001);
begin
insert into student values(1,'ABC','AHM');
exception
when primary_ky then
dbms_output.put_line('Can not add values');
end;
/




3. When you perform update or delete operation on table company1, stores the updated or deleted                values in table company2. Also show the operation ‘update’ and ‘delete’ and username.

     Table: Company1 (no, name)
                Company2 (no,name , operation, username)
Ans:- 
create or replace trigger Comp after update or delete on Company1 for each row

declare
no number(1);
name varchar2(20);
username varchar2(20);
op varchar2(10);                
begin
if updating then
op:='UPDATE';
no:=:OLD.No;
name:=:OLD.Name;
end if;
if deleting then
op:='DELETE';
no:=:OLD.No;
name:=:OLD.Name;
end if;
insert into Company2 values(no,name,op,USER);
end;
/

4. Whenever you insert any value into table test1 then table test2 automatically calculate the Total    of No 1 and No 2.

Table:  Test1 (No1,No2)   
          Test2 (Total)
Ans:-

create table Test1(a number,b number);
create table Test2(c number);

create or replace trigger Add_numb1 after insert on Test1 for each row
declare
a number;
b number;
c number;
begin
c:=:new.a+:new.b;
insert into Test2 values(c);
end;
/
  
insert into Test1 values(1,2);

select * from Test2;

Comments

Post a Comment

Popular posts from this blog

SQL QUERY FOR PRIMARY AND FOREIGN KEY

SQL QUERY

SQL QUERY practical-5

Practicals of RollBack, Commit and Cursor programs