Practicals of RollBack, Commit and Cursor programs


                                                               Practical-10

AIM :- RollBack  , Commit and Cursor bases Programs.

1. Analyze output of following scripts:
A.     Begin
                              Insert into new_salary values ('6', '6000');
                              Update new_salary set salary=4000 where id=4;
                              Commit;
                              Rollback;
              End;
              /
Ans:-


               B.  Begin
              
                 Insert into new_salary values (8, 8000);
               Update new_salary set salary=50000 where id=5;
               Savepoint  first; 
                Rollback to savepoint first;
       End;
       /             
Ans:-


2. Write pl/sql blocks that first insert the new record into table salary. Update the salary of employee name ‘Riya’ and ‘Rima’ by 7000/- then check the total salary  which does not exceed 20000/-.if it happens then UNDO  the update and give the  error message “TOTAL SALARY EXCEED 20000”.

       Table : Salary

Emp_no
Name
Salary
1001
Riya
5000
1002
Rima
10000
1003
Rekha
12000
1004
Dhara
6000







Ans:-
   declare

   sal number(8,2);                   
  begin
  savepoint first;
  update Salary set salary=7000 where name='Riya' or name='Rima';                  
  select sum(salary) into sal from salary;
  ifsal>20000 then
  rollback to savepoint first;
  dbms_output.put_line('TOTAL SALARY EXEEDED 20000');
  end if;
  end;
  /

select * from salary;


3. Write a pl/sql to give 5% raise to every employee.

 Use Table Salary.

Ans:- 

declare
cursor cursor1 is select * from SALARY;
emp_no number(10);
name varchar2(10);
salary number(10);
begin
open cursor1;
loop
fetch cursor1 into emp_no,name,salary;
exit when cursor1%notfound;
dbms_output.put_line('emp_no is'||emp_no);
dbms_output.put_line('name is'||name);
dbms_output.put_line('salary is'||salary);

end loop;
update SALARY set salary=salary+(salary*0.05);
close cursor1;
end;
/

4. Write a pl/sql block that find the table empmaster and stores the data into another table CITYLIST(no,name) where CITY=’Bombay’ . Create this pl/sql using following two:
Use pl/sql data type %TYPE
Use pl/sql data %ROWTYPE.
Table: Empmaster

No
Name
City
10
Riya
Bombay
20
Rima
Delhi
30
Rekha
Bombay
40
Dhara
Ahemdabad
50
Dhaval
Jaypur









Ans:- 

declare
cursor cursor2 is select * from Empmaster_1 where City='Bombay';
emp_rec cursor2 %ROWTYPE;
begin
open cursor2;
loop
fetch cursor2 into emp_rec;
exit when cursor2 %NOTFOUND;
dbms_output.put_line('No is'||emp_rec.No);
dbms_output.put_line('Name is'||emp_rec.Name);
dbms_output.put_line('City is'||emp_rec.City);
insert into citylist_1 values(emp_rec.NO,emp_ec.Name);
end loop;
close cursor2;
end;
/

select * from Citylist_1;

5.Write a pl/sql block that update SALARY of employee of a specified company Using parameter in Cursor.
Table : Temp

No
Company
Salary
01
INFOSYS
18000
02
TCS
22000
03
WIPRO
20000
04
ORACLE
25000






Create table Temp_1(No number(2),Company varchar2(15),Salary number(8));

insert into Temp_1 values(01,'INFOSYS',18000);
insert into Temp_1 values(02,'TCS',22000);
insert into Temp_1 values(03,'WIPRO',20000);
insert into Temp_1 values(04,'ORECAL',25000);



Ans:-
 declare

cursor c6(compa varchar2) is select * from Temp_1  where Company=compa;
rec c6%rowtype;
compa varchar2(10);
sal number;
begin
compa:='&compa';
sal:=&sal;
open c6(compa);
if c6 %isopen then
loop
fetch c6 into rec;
exit when c6 %notfound;
update Temp_1 set Salary=sal where Company=rec.Company;
end loop;
commit;
end if; 
close c6;
end;
/

6. Display Only NO,NAME of First three row . Use table Empmaster ( Use FOR LOOP)
Ans:-


declare
cursor cursor4 is select * from Empmaster_1;
No number;
Name varchar2(20);
City varchar2(20);
begin
open cursor4;
loop
fetch  cursor4 into No,Name,City;
 exit when cursor4 %ROWCOUNT>3;
dbms_output.put_line('No’||’       ‘||’Name’);
dbms_output.put_line('---'||'     '||'---');
 end loop;
 end;
 /

Comments

Post a Comment

Popular posts from this blog

SQL QUERY FOR PRIMARY AND FOREIGN KEY

SQL QUERY

SQL QUERY practical-5

Programs Based on Exception Handling and Trigger.