while salTotal <= 50000
loop
fetch c1 into pempno, psal;--È¡³öÒ»Ìõ¼Ç¼
exit when c1%notfound;
update emp set sal = sal * 1.1 where empno = pempno; --Ö´ÐмÓн
--¼Ç¼Õǹ¤×ʺóµÄ×ܶî
salTotal := salTotal + psal*0.1;
--¼Ç¼Õǹ¤×ʵÄÈËÊý
empCount := empCount + 1;
end loop;
close c1;
commit;
dbms_output.put_line('Õǹ¤×ÊÈËÊý:' || empCount || ' ¹¤×Ê×ܶî:' || salTotal);
end;
/
ʾÀý2
lÓÃPL/SQLÓïÑÔ±àдһ³ÌÐò£¬ÊµÏÖ°´²¿ÃŷֶΣ¨6000ÒÔÉÏ¡¢(6000£¬3000)¡¢3000ÔªÒÔÏÂ)ͳ¼Æ¸÷¹¤×ʶεÄÖ°¹¤ÈËÊý¡¢ÒÔ¼°¸÷²¿ÃŵŤ×Ê×ܶî(¹¤×Ê×ܶîÖв»°üÀ¨½±½ð)£¬²Î¿¼Èçϸñʽ£º
²¿ÃŠСÓÚ3000Êý 3000-6000 ´óÓÚ6000 ¹¤×Ê×ܶî
10 2 1 0 8750
20 3 2 0 10875
30 6 0 0 9400
?
lÌáʾ£º¿ÉÒÔ´´½¨Ò»ÕÅбíÓÃÓÚ±£´æÊý¾Ý
?
createtable msg1
(deptno number,
emp_num1 number,
emp_num2 number,
emp_num3 number,
sum_salnumber);
/*
ÓÃPL/SQLÓïÑÔ±àдһ³ÌÐò£¬ÊµÏÖ°´²¿ÃŷֶΣ¨6000ÒÔÉÏ¡¢(6000£¬3000)¡¢3000ÔªÒÔÏÂ)ͳ¼Æ¸÷¹¤×ʶεÄÖ°¹¤ÈËÊý¡¢
ÒÔ¼°¸÷²¿ÃŵŤ×Ê×ܶî(¹¤×Ê×ܶîÖв»°üÀ¨½±½ð)
ÏÈд³ö¿ÉÄÜÓõ½µÄ²éѯÓï¾ä
a = select distinct deptno from dept;
select sal from emp where deptno= aÖеÄij¸öÖµ;
¹ØÓÚ½á¹ûµÄÊä³ö£º
1. Ö±½ÓÊä³öÔÚÆÁÄ»ÉÏ
2. Êä³öµ½Ò»ÕűíÖÐ
create table salcount
(deptno number, --²¿ÃźÅ
sg1 int, --3000ÒÔϵÄÈËÊý
sg2 int, -- 3000~6000µÄÈËÊý
sg3 int -- 6000ÒÔÉϵÄÈËÊý
);
*/
declare
--¶¨ÒåÁ½¸öÓα걣´æ½á¹û
cursor c1 is select distinct deptno from dept;
cursor c2(pdno number) is select sal from emp where deptno=pdno;
--¶¨ÒåÈý¸ö±äÁ¿ÓÃÓÚ±£´æÃ¿¸ö²¿ÃÅÈý¸ö¹¤×ʶεÄÈËÊý
count1 NUMBER;
count2 number;
count3 number;
--¼Ç¼c1ÓαêÖеIJ¿ÃźÅ
pdeptno dept.deptno% TYPE;
--¼Ç¼c2ÓαêÖеÄнˮֵ
psal emp.sal% TYPE;
begin
open c1;--´ò¿ªc1 »ñµÃËùÓв¿ÃźÅ
loop
fetch c1 into pdeptno;--ȡһ¸ö²¿ÃźÅ
exit when c1%notfound;
--¼ÆÊýÆ÷ÇåÁã
count1 := 0;
count2 := 0;
count3 := 0;
--µÃµ½¸Ã²¿ÃŵÄËùÓÐÔ±¹¤
open c2(pdeptno);
loop
fetch c2 into psal; --µÃµ½¸ÃÔ±¹¤µÄ¹¤×Ê
exit when c2%notfound;
if psal <=3000 then count1 := count1 + 1;
elsif psal > 3000 and psal <=6000 then count2 := count2 + 1;
else count3 := count3 + 1;
end if;
end loop;
close c2;
--±£´æ¸Ã²¿ÃŵÄͳ¼Æ½á¹û
insert into salcount values(pdeptno,count1,count2,count3);
commit;
end loop;
close c1;
end;
/