oracle 总结 创建临时表 会话临时表和事物临时表区别 编写存储过程以及调用临时表demo

直接贴代码了啊 ,逻辑是实时统计每个业务员的团队佣金并且将统计的数据展示在页面上

通过java代码会比较影响数据库资源,所以我使用了存储过程、临时表逻辑。期间学习的总结一下

第一   会话临时表 (说明临时表是会话指定,当中断会话时ORACLE将截断表,删除所有数据)

第二   事物临时表 (说明临时表是事务指定,每次提交后ORACLE将截断表,删除所有数据)

我使用的是会话临时表,因为我需要将统计的佣金插入临时表中,并返回查询显示结果

-------创建会话级临时表  
create global temporary table fht_team_reward_commissiontem (
        id number,
        username varchar2(20),
        teamName varchar2(60),
        commission number,
        premium number,
        twolevelpremium number,
        twolevelcommission number,
        threelevelpremium number,
        threelevelcommission number,
        levelcommission number,
        teampolicys number,
        userid number,
        createtime DATE
    )   On Commit Preserve Rows;

-------创建存储过程

create or replace procedure mydemo09( cityid in varchar2 ,p_cur out sys_refcursor ,userName in varchar2 )
is
ra number := cityid ;------城市id
levelCommission number ;----等级佣金
teamCommission number;----团队佣金
reCommission number ;---返点佣金
premium number ;----自营保费
twoLevelPremium number ;-----二级团队总保费
threeLevelPremium number ;-----三级团队总保费
twoLevelRebate varchar2(255) ;------二级团队返点
threeLevelRebate varchar2(255) ;----三级团队返点
twoLevelCommission number;----二级佣金
threeLevelCommission number;----三级佣金
teamPolicys varchar2(255) ;----团队总保单数量
begin
  levelCommission := 0 ;
 for user in (select u.* from fht_users u , fht_enterprise e where u.enterpriseid = e.id and e.cityid=ra and u.gradetype is not null ) loop
  -----个人返点佣金
  select sum(reCommission) into reCommission from fht_level_commission where currentUserName=user.userName ;
   -----等级佣金
  select sum(commission) into levelCommission from fht_level_commission where directlySuperiorUserName=user.userName ;
  -----自营保费
  select sum(totalPremium) into premium from fht_level_commission where currentUserName=user.userName ;
  -----本月二级用户所做的总保费
  select sum(totalPremium) into twoLevelPremium from fht_level_commission where currentUserName in (select  tm.userName  from (  select m.*, level leaf   from fht_teamUsers m start with m.teamLeaderId = user.userid connect by m.teamLeaderId = prior m.userId  ) tm where tm.leaf = 1 );
  -----本月三级用户所做的总保费
  select sum(totalPremium) into threeLevelPremium from fht_level_commission where currentUserName in (select  tm.userName  from (  select m.*, level leaf   from fht_teamUsers m start with m.teamLeaderId = user.userid connect by m.teamLeaderId = prior m.userId  ) tm where tm.leaf = 2)  ;
  ------二级团队返点
  select teamSecondRebate into twoLevelRebate from fht_teamConfigurationRatio where cityid=ra and del=0 ;
  ----三级团队返点
  select teamThirdRebate into threeLevelRebate from fht_teamConfigurationRatio where cityid=ra and del=0 ;
  ----二级佣金
  select countTeamCommission(twoLevelPremium,twoLevelRebate) into twoLevelCommission from dual ;
  select countteamcommission(threeLevelPremium ,threeLevelRebate) into threeLevelCommission from dual ;
  teamCommission := twoLevelCommission + threeLevelCommission  ;
  ----团队总保单数量
  select count(id) into teamPolicys from fht_level_commission where currentUserName in (
      select tm.userName from (select m.*, level leaf  from fht_teamUsers m start with m.teamLeaderId = user.userid connect by m.teamLeaderId = prior m.userId) tm where tm.leaf = 1
      union all
      select username from fht_users  where userid = user.userid
      union all
      select tm.userName from (select m.*, level leaf  from fht_teamUsers m  start with m.teamLeaderId = user.userid connect by m.teamLeaderId = prior m.userId) tm  where tm.leaf = 2
   )  ;

 
  ---------不管本月数据是否在团队佣金临时表中是否存在,直接删除相关数据,从新存储
  delete from fht_team_reward_commissiontem where userName = user.userName ;
  ----插入数据
  insert INTO  fht_team_reward_commissiontem (
         id,username,teamName ,commission,premium,twolevelpremium ,twolevelcommission ,threelevelpremium,threelevelcommission,
         levelcommission,teampolicys,userid ,createtime
         )
  values (hibernate_sequence.nextval ,user.userName ,user.realName,teamCommission ,premium ,twoLevelPremium,twoLevelCommission,threeLevelPremium,threeLevelCommission ,
         levelcommission,teamPolicys,user.userid,sysdate
         );
  end loop;
 open p_cur for 'select * from fht_team_reward_commissiontem where userName like  ' || '''' || '%' || userName || '%' || '''   or  teamName like  ' || '''' || '%' || userName || '%' || ''' order by commission desc';
 commit;
end;

存储过程中用到了一个函数给一下

CREATE OR REPLACE FUNCTION countTeamCommission(vid in VARCHAR2 , rebate IN VARCHAR2)
------根据 传入的 保费总金额 和 相关 二级配置返点 计算所得佣金 的函数
return VARCHAR2
is
 firstParam varchar2(25);
 firstrate varchar2(25);
 premium number := 0;
begin

    for rateArr in (select column_value from table(split(rebate,','))) loop
       select substr(column_value,2) into firstParam from table(split(rateArr.column_value,'=')) where rownum<=1 ;
       select column_value into firstrate from table(split(rateArr.column_value,'=')) where rownum<=2 minus select column_value from table(split(rateArr.column_value,'=')) where rownum<=1;
       if to_number(vid) <= to_number(firstParam) then
         premium := firstrate * vid ;
         EXIT;
       END IF;
    end loop ;
    return  premium;
 end;
 

这个函数根据保费 以及返点来计算佣金

select countteamcommission(100000,'<1000000=0.01,<2000000=0.12') from dual

上面函数用到了分割字符串函数,给一下源码

创建一个类型  create or replace type type_str is table of varchar2(100) ;

create or replace function split(p_str varchar2,p_delimiter varchar2 default ',') return type_str

is

  rs type_str:=type_str();

  l_str varchar2(4000):='';

  l_len number:=0;

begin

  l_str:=p_str;

  l_len:=length(p_delimiter);

  while length(l_str)>0 loop

     if instr(l_str,p_delimiter)>0 then

       rs.extend;

       rs(rs.count):=substr(l_str,1,instr(l_str,p_delimiter)-1);

       l_str:=substr(l_str,instr(l_str,p_delimiter)+l_len);

     else

       rs.extend;

       rs(rs.count):=l_str;

       exit;

     end if;

  end loop;

  return rs;

end;
 

存储过程的调用

declare
  p_cur sys_refcursor;
  a     fht_team_reward_commissiontem%rowtype;
begin
  mydemo09('3465', p_cur ,'');

  loop
    fetch p_cur
      into a;
    exit when p_cur%notfound;
   dbms_output.put_line(a.userName || '---' || '---' || a.twoLevelPremium);
  end loop;
  close p_cur;
end;

-------使用到了游标

p_cur out sys_refcursor 游标参数    open p_cur for   循环打开游标查询的结果

猜你喜欢

转载自blog.csdn.net/qq_28917403/article/details/86612789