游标、动态sql、异常

游标分,静态游标和动态游标

静态游标:在编译的时候就知道其select语句的游标

动态游标:运行的时候动态决定执行何种查询

游标的使用步骤

 游标的属性

--计算个税 
declare
--声明游标
 cursor cursor_teacher is
 select tname,sal from teacher ;
 v_teacher_record cursor%rowtype;
 v_tax number:=0;
 v_tax_sum number:=0;
 v_sal number;
begin
 open cursor_teacher;
     loop 
     fetch cursor_teacher into v_teacher_record;
     exit when cursor_teacher%notfound;
     --开始计算个税  
     v_tax:=0;
     v_sal:=v_teacher_record.sal;
     if v_sal>=5000 and v_sal<10000 then
         v_tax:=(v_sal-5000)*0.05;
         elsif v_sal>=10000 and v_sal<40000 then
         v_tax:=(v_sal-10000)*0.1+5000*0.05;
         elsif v_sal>=40000 then
         v_tax:=(v_sal-40000)*0.3+30000*0.1+5000*0.05;
     end if;    
     v_sum:=v_tax_sum+v_tax;
     dbms_output.put_line(v_teacher_record.tname||'的个税:'||v_tax);
     end loop;
 dbms_output.put_line('个税总额:'||v_tax_sum);
 close cursor_teacher;
 
 
 v_tax_sum:=0;
 for v_teacher_record in cursor_teacher loop
   v_tax:=0;--清0
     v_sal=v_teacher_record.sal;
     if v_sal>=5000 and v_sal<10000 then
         v_tax:=(v_sal-5000)*0.05;
         elsif v_sal>=10000 and v_sal<40000 then
         v_tax:=(v_sal-10000)*0.1+5000*0.05;
         elsif v_sal>=40000 then
         v_tax:=(v_sal-40000)*0.3+30000*0.1+5000*0.05;
     end if;    
     v_tax_sum:=v_tax_sum+v_tax;
 end loop;
end;

以上的税率也可以设置为常量来做

c_tax1 constant number:=0.1;

动态sql实现由2种实现方法:

你可能感兴趣的:(游标、动态sql、异常)