通过实际的例子学习Oracle存储过程

--创建存储过程
CREATE OR REPLACE PROCEDURE xxxxxxxxxxx_p
(
--参数IN表示输入参数,OUT表示输入参数,类型可以使用任意Oracle中的合法类型。
 is_ym  IN CHAR
)
AS
--定义变量
 vs_msg   VARCHAR2(4000);   --错误信息变量
 vs_ym_beg  CHAR(6);      --起始月份
 vs_ym_end  CHAR(6);      --终止月份
 vs_ym_sn_beg CHAR(6);     --去年同期起始月份
 vs_ym_sn_end CHAR(6);     --去年同期终止月份
--定义游标(简单的说就是一个可以遍历的结果集)
 CURSOR cur_1 IS
 SELECT area_code,CMCODE,SUM(rmb_amt)/10000 rmb_amt_sn,SUM(usd_amt)/10000 usd_amt_sn
 FROM BGD_AREA_CM_M_BASE_T
  WHERE ym >= vs_ym_sn_beg
  AND ym <= vs_ym_sn_end
 GROUP BY area_code,CMCODE;             
  rec  cur_1%rowtype;
BEGIN
 --用输入参数给变量赋初值,用到了Oralce的SUBSTR TO_CHAR ADD_MONTHS TO_DATE 等很常用的函数。
 vs_ym_beg := SUBSTR(is_ym,1,6);
 vs_ym_end := SUBSTR(is_ym,7,6);
 vs_ym_sn_beg := TO_CHAR(ADD_MONTHS(TO_DATE(vs_ym_beg,'yyyymm'), -12),'yyyymm');
 vs_ym_sn_end := TO_CHAR(ADD_MONTHS(TO_DATE(vs_ym_end,'yyyymm'), -12),'yyyymm');
 --先删除表中特定条件的数据。
 DELETE FROM xxxxxxxxxxx_T WHERE ym = is_ym;
  --然后用内置的DBMS_OUTPUT对象的put_line方法打印出影响的记录行数,其中用到一个系统变量SQL%rowcount
 DBMS_OUTPUT.put_line('del上月记录='||SQL%rowcount||'条');
 
 INSERT INTO xxxxxxxxxxx_T(area_code,ym,CMCODE,rmb_amt,usd_amt)
 SELECT area_code,is_ym,CMCODE,SUM(rmb_amt)/10000,SUM(usd_amt)/10000
 FROM BGD_AREA_CM_M_BASE_T
  WHERE ym >= vs_ym_beg
  AND ym <= vs_ym_end
 GROUP BY area_code,CMCODE;
 
 DBMS_OUTPUT.put_line('ins当月记录='||SQL%rowcount||'条');
 --遍历游标处理后更新到表。遍历游标有几种方法,用for语句是其中比较直观的一种。
 FOR rec IN cur_1 LOOP
  UPDATE xxxxxxxxxxx_T
  SET rmb_amt_sn = rec.rmb_amt_sn,usd_amt_sn = rec.usd_amt_sn
   WHERE area_code = rec.area_code
   AND CMCODE = rec.CMCODE
   AND ym = is_ym;
 END LOOP;
 
 COMMIT;
 --错误处理部分。OTHERS表示除了声明外的任意错误。SQLERRM是系统内置变量保存了当前错误的详细信息。
EXCEPTION
   WHEN OTHERS THEN
      vs_msg := 'ERROR IN xxxxxxxxxxx_p('||is_ym||'):'||SUBSTR(SQLERRM,1,500);
   ROLLBACK;
   --把当前错误记录进日志表。
   INSERT INTO LOG_INFO(proc_name,error_info,op_date)
   VALUES('xxxxxxxxxxx_p',vs_msg,SYSDATE);
   COMMIT;
   RETURN;
END;

 ==========================================================================

CREATE  TABLE T_Temp_XinQiYue (
incoming varchar2(10),
total_list number,
done_list number,
book_list number,
ten_done_rate varchar2(10)
) ;


CREATE OR REPLACE PROCEDURE CREATE_XinQiYue_REPORT
(
    starttime in varchar2,
    endtime in varchar2,
    cur_arg out sys_refcursor

)as
  sqlstr varchar2(200);
  startdate varchar2(50);
  enddate varchar2(50);
  channel varchar2(50);    --存储游标值
  tatol number;  --应访件数
  tatol10 number;  --10日内回访成功件数
  tatol10All number:=0; --10日内回访成功件数总数
  cursor c_customer is select distinct customer_53 from t_bus_customer;
begin
   startdate := starttime;
   enddate := endtime;
   sqlstr:='delete from T_Temp_XinQiYue';
   execute immediate sqlstr;
   commit;
   
    open c_customer;
   loop
   fetch c_customer into channel;
   exit when c_customer%notfound;
  
   if channel is null then  --值为空
         --获得应访件数
				  select count(1) into tatol from t_bus_customer 
				  where activity_id='4028802629de692b0129e9eebd280034' and customer_53 is null 
				        and (input_time >=startdate and input_time <=enddate or input_time is null)
				        and customer_status not in ('10','30');
				  
				  --10日内回访成功件数
				  select count(1) into tatol10 from t_bus_customer 
				  where activity_id='4028802629de692b0129e9eebd280034' and customer_53 is null 
				  and to_date(substr(calling_time,0,10),'yyyy-mm-dd')-to_date(substr(input_time,0,10),'yyyy-mm-dd') <=10 
				  and customer_status in( '212','222') and  (input_time >=startdate and input_time <=enddate or input_time is null)
				  and customer_status not in ('10','30');
				  
				  --累计加上10日内回访成功件数
				  tatol10All:=tatol10All+tatol10;
				
				  insert into T_Temp_XinQiYue (incoming,total_list,done_list,book_list,ten_done_rate) 
					values ('',tatol,
					(select count(1) from t_bus_customer where activity_id='4028802629de692b0129e9eebd280034' and customer_53 is null and customer_status in( '212','222') and (result_id !='40284b982ae26035012ae634576d0347' or result_id is null) and  (input_time >=startdate and input_time <=enddate or input_time is null)and customer_status not in ('10','30')),
					(select count(1) from t_bus_customer where activity_id='4028802629de692b0129e9eebd280034' and customer_53 is null and result_id ='8a1186a1208125c40120847200f518ce' and  (input_time >=startdate and input_time <=enddate or input_time is null)and customer_status not in ('10','30')),
					case tatol
					when 0 then '0%'
					else
					substr(tatol10/tatol*100,0,5)||'%'
					end
					);
   else                --值不为空
			   --获得应访件数
			   select count(1) into tatol from t_bus_customer where activity_id='4028802629de692b0129e9eebd280034' and customer_53 =channel and   (input_time >=startdate and input_time <=enddate or input_time is null) and customer_status not in ('10','30');
			    --10日内回访成功件数
			   select count(1) into tatol10 from t_bus_customer where activity_id='4028802629de692b0129e9eebd280034' and customer_53 =channel and to_date(substr(calling_time,0,10),'yyyy-mm-dd')-to_date(substr(input_time,0,10),'yyyy-mm-dd') <=10 and customer_status in( '212','222')and (input_time >=startdate and input_time <=enddate or input_time is null) and customer_status not in ('10','30');
			     --累计加上0日内回访成功件数
			  tatol10All:=tatol10All+tatol10;
			
			   insert into T_Temp_XinQiYue (incoming,total_list,done_list,book_list,ten_done_rate) 
			   values (channel,tatol,
			   (select count(1) from t_bus_customer where activity_id='4028802629de692b0129e9eebd280034' and customer_53 =channel and customer_status in( '212','222') and (result_id !='40284b982ae26035012ae634576d0347' or result_id is null) and   (input_time >=startdate and input_time <=enddate or input_time is null) and customer_status not in ('10','30')),
			   (select count(1) from t_bus_customer where activity_id='4028802629de692b0129e9eebd280034' and customer_53 =channel and result_id ='8a1186a1208125c40120847200f518ce'and   (input_time >=startdate and input_time <=enddate or input_time is null) and customer_status not in ('10','30')),
			  case tatol
			  when 0 then '0%'
			  else
			  substr(tatol10/tatol*100,0,5)||'%'
			  end
			  );
   end if;
   
   end loop;

   select sum(total_list) into tatol 
   from T_Temp_XinQiYue;
     if tatol>0 then
        insert into T_Temp_XinQiYue (incoming,total_list,done_list,book_list,ten_done_rate)
        select '总计',sum(total_list),sum(done_list),sum(book_list),substr(tatol10All/sum(total_list)*100,0,5)||'%'  from T_Temp_XinQiYue;
     else
        insert into T_Temp_XinQiYue (incoming,total_list,done_list,book_list,ten_done_rate) 
        select '总计',sum(total_list),sum(done_list),sum(book_list),'0%'  from T_Temp_XinQiYue;
     end if;
     
     close c_customer;
     
     open cur_arg for select * from T_Temp_XinQiYue;

end;

猜你喜欢

转载自lpm528.iteye.com/blog/1563958