0% found this document useful (0 votes)
21 views

PLSQL

The final document
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

PLSQL

The final document
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

cursor to update salaries for sailors

1. 500 for age>=40


2. 300 for age>30 and <40
3. 100 for else
declare
srec sailors%rowtype;
cursor c1 is select * from sailors;
begin
open c1;
loop
fetch c1 into srec;
exit when c1%notfound;
if srec.age>=40 then
update sailors set salary=salary+500 where sid=srec.sid;
elsif srec.age>=30 and srec.age<40 then
update sailors set salary=salary+300 where sid=srec.sid;
else
update sailors set salary=salary+100 where sid=srec.sid;
end if;
end loop;
close c1;
end;

cursor to update salaries for sailors


1. 500 for rating>=10
2. 300 for rating>=8 and <10
3. 100 for else
declare
srec sailors%rowtype;
cursor c1 is select * from sailors;
begin
open c1;
loop
fetch c1 into srec;
exit when c1%notfound;
if srec.rating=10 then
update sailors set salary=salary+500 where sid=srec.sid;
elsif srec.rating>=8 and srec.rating<10 then
update sailors set salary=salary+300 where sid=srec.sid;
else
update sailors set salary=salary+100 where sid=srec.sid;
end if;
end loop;
close c1; end;
while cursor

declare
srec sailors%rowtype;
cursor c1 is select * from sailors;
begin
open c1;
fetch c1 into srec;
while c1%found
loop
if srec.age>=40 then
update sailors set salary=salary+500 where sid=srec.sid;
elsif srec.age>=30 and srec.age<40 then
update sailors set salary=salary+300 where sid=srec.sid;
else
update sailors set salary=salary+100 where sid=srec.sid;
end if;
fetch c1 into srec;
end loop;
close c1;
end;

cursor – for loop


declare
cursor c1 is select * from sailors;
begin
for srec in c1
loop
if srec.age>=40 then
update sailors set salary=salary+500 where sid=srec.sid;
elsif srec.age>=30 and srec.age<40 then
update sailors set salary=salary+300 where sid=srec.sid;
else
update sailors set salary=salary+100 where sid=srec.sid;
end if;
end loop;
end;
Mytrig1.sql
create or replace trigger mytrig
after insert on sailors
begin
dbms_output.put_line('trigger executed');
end;

Mytrig2.sql
CREATE OR REPLACE TRIGGER Print_salary_changes
BEFORE DELETE OR INSERT OR UPDATE ON sailors
FOR EACH ROW
DECLARE
sal_diff number;
BEGIN
sal_diff := :NEW.SALARY - :OLD.SALARY;
dbms_output.put('Old salary: ' || :OLD.salary);
dbms_output.put(' New salary: ' || :NEW.salary);
dbms_output.put_line(' Difference ' || sal_diff);
END;
/

You might also like