---
title: "存储过程实例"
author: "charles751"
date: "2008-12-24T09:06:51Z"
tags: []
categories: ["企业架构"]
slug: "untitled"
draft: false
lastmod: "2008-12-24T09:06:51Z"
id: 300125
url: "https://qify.iteye.com/blog/300125"
---

下面这段存储过程实现的逻辑：

统计昨天注册的新用户的数据，插入到指定的表中，提供给应用程序查询，生成报表。

 

 

CREATE OR REPLACE PROCEDURE agent\_reguser\_static  
\-- 统计昨天的用户注册信息：                                                                         
                                                                                            
is                                                                                                                             
ls\_user\_id number(10);                                                                                                
ls\_remarks varchar2(10);                                                                                                      
ls\_websiteid varchar2(15);  
ls\_salesid varchar2(150);    
ls\_agentid varchar2(15);                                                                                                                             
                                                                                                                               
begin  
 delete from agent\_reg\_user where to\_char(time,'yyyy-mm-dd') = to\_char( sysdate-1,'yyyy-mm-dd' );--删除掉历史数据，重新统计  
 commit;  
   
 declare  cursor cur\_reg\_user is    --统计昨天新注册的用户     
  select distinct w.userid,w.sitevalue from web\_listen w,agent\_user\_info a where to\_char(w.regdate,'yyyy-mm-dd')  
   =to\_char( sysdate-1,'yyyy-mm-dd') and ((w.sitevalue = a.agentid and a.agentlevel = 3) or (w.sitevalue like 'lerss%'));  
      
 begin  
  open cur\_reg\_user;  
        loop  
         fetch cur\_reg\_user into ls\_user\_id, ls\_websiteid;  
         exit when cur\_reg\_user%notfound;

  if substr(ls\_websiteid,1,5) ='lerss' then  
   ls\_salesid := '080829005000001';  
   ls\_agentid := '080829005';  
  else    
   select agentid,parentid into ls\_salesid,ls\_agentid from agent\_user\_info where agentid in( select parentid from agent\_user\_info  
    where agentid = ls\_websiteid and agentlevel = 3);  
  end if;  
      
  --将注册信息插入到表agent\_reg\_user中  
  insert into agent\_reg\_user(id,userid,username,truename,cardnum,mobile,email,area,time,websiteid,salesid,agentid)  
   
  select SEQ\_AGENT\_REG\_USER.nextval,ls\_user\_id,username,truename,cardid,phone,email,substr(cardid,1,2)||'0000',  
   time,ls\_websiteid,ls\_salesid,ls\_agentid from luser where id = ls\_user\_id;  
  commit;

 end loop ;  
 close cur\_reg\_user;                                                                                                 
      
  --更新area信息，如果不属于基本省的地区信息，则均属于其它  
  update agent\_reg\_user set area = '100000' where to\_char(time,'yyyy-mm-dd') = to\_char( sysdate-1,'yyyy-mm-dd' )  
   and area not in  (select area\_id from agent\_area\_dic);  
  commit;  
end;  
                                                                                                                           
end agent\_reguser\_static;  
/