我是搞ASP的,但有个问题在ASP里处理不了,因为数据量的原因,导制ASP超时。所以想用SQL的存储过程用定期的形式来完成ASP里的计算
具体是这样的。表 user1列说明:dm会员卡号,name会员姓名 pid会员所属上级推荐会员   xf会员每周消费金额 yj为会员所有下级会员的消费金额的累计,比如下面的四条记录,第一个会员的累计就是500,第二个就是400,第三个就是200。这个消费金额是在不断更新的,所以要求后面的yj内的数值也要定期更新。
在ASP里我用递归计算,有几万条数据跑到超时,所以在此求救,如可以解答,送分400。送分400!送分400。送分400!送分400。送分400!送分400。送分400!送分400。送分400!送分400。送分400!送分400。送分400!送分400。送分400!列 dm(int)   name(varchar)   pid(int)   xf(int)    yj(int)
   1         1               1          100        500
   2         2               1          200        400
   3         3               2          100        200
   4         4               3          100        100
.....
pid值和dm值相同的会员为没有推荐人的顶级会员。。
 
 
要求实现如下效果
每周自动计算一次,任意会员所有下级会员消费金额累计到yj列内这里好像有点你传销,但不是的,是一个俱乐部的会员系统。大家帮着一下好吗?我对SQL的存储过程与作业都不懂,如有把握可以解决请留下QQ,如有困难,愿出人民币求助~

解决方案 »

  1.   

    declare @one table (a int)
    declare @bb int
    insert into @one select yj from user1 as t1 inner join user1 as t2 on t1.dm=t2.pid
    set @bb=sum(select a from @one)
    update user1
    set yj=@bb
    在作业里面执行上面的语句,我想应该可以吧,我也是一个菜鸟啊,
    有机会的话明天确认了再跟你留言啊~~
      

  2.   

    SQL的用法我一点也不懂,说实话,这些写在哪里我都不知道,所以请留下你的QQ,或MSN,我好与你联系,不会亏待你的~~如有ASP要开发的话,可以帮你开发免费~~
      

  3.   

    --计算的存储过程
    create proc p_calc
    as--初始化 yj 字段
    update 表 set yj=0--分级计算
    update 表 set yj=xf
    from 表 a
    where not exists(
    select 1 from 表 where pid=a.dm and dm<>pid)
    while @@rowcount>0
    update 表 set yj=isnull(a.xf,0)+isnull(b.yj,0)
    from 表 a join(
    select pid,yj=sum(yj) from 表
    where yj<>0 group by pid
    )b on a.dm=b.pid
    where a.yj=0 and not exists(
    select 1 from 表 where pid=a.dm and yj=0 and dm<>pid)
    go--调用存储过程进行计算
    exec p_calc
      

  4.   

    --测试--测试数据
    create table 表(dm int,name varchar(10),pid int,xf int,yj int)
    insert 表 select 1,1,1l,100,500
    union all select 2,2,1,200,400
    union all select 3,3,2,100,200
    union all select 4,4,3,100,100
    go--计算的存储过程
    create proc p_calc
    as--初始化 yj 字段
    update 表 set yj=0--分级计算
    update 表 set yj=xf
    from 表 a
    where not exists(
    select 1 from 表 where pid=a.dm and dm<>pid)
    while @@rowcount>0
    update 表 set yj=isnull(a.xf,0)+isnull(b.yj,0)
    from 表 a join(
    select pid,yj=sum(yj) from 表
    where yj<>0 group by pid
    )b on a.dm=b.pid
    where a.yj=0 and not exists(
    select 1 from 表 where pid=a.dm and yj=0 and dm<>pid)
    go--调用存储过程进行计算
    exec p_calc--显示计算结果
    select * from 表
    go--删除测试
    drop table 表
    drop proc p_calc/*--测试结果dm          name       pid         xf          yj          
    ----------- ---------- ----------- ----------- ----------- 
    1           1          1           100         500
    2           2          1           200         400
    3           3          2           100         200
    4           4          3           100         100(所影响的行数为 4 行)
    --*/
      

  5.   

    我觉得没有必要写成定时计算,你在需要计算的时候,用asp调用存储过程就行了<%
    conn="数据库连接字符串"
    set db=server.createobject("adodb.connection")
    db.open conn
    db.execute "exec p_calc"
    db.close
    %>
      

  6.   

    我加了几万条的数据后,用ASP调用存储过程,还是很慢,怎样可以解决这个速度的问题呢?
      

  7.   

    ASP执行到超时,不过数据在数据库里都累计出来了,这对客户来说会很不能理解的。请问有什么好的办法吗?比如用作业会不会好一点?或其它的什么好办法?
      

  8.   

    dm,pid字段上分别建立索引asp调用改为:
    <%
    conn="数据库连接字符串"
    set db=serve.createobject("adodb.connection")
    db.connectimeout=0
    db.commandtimeout=0
    db.open conn
    db.execute "exec p_calc"
    db.close
    %>
      

  9.   

    用作业当然可以,你对SQL不熟,作业调度出问题你怎么维护?
      

  10.   

    可是INT类型的怎么建立索引呀?我不会呀
      

  11.   

    对象不支持已命名参数: 'db.ConnectTimeout' 
    执行后出错提示~~再次打扰请教
      

  12.   

    可是INT类型的怎么建立索引呀?我不会呀怎么建立索引呀,不是好像只有CHAR,VARCHAR才能建索引吗?再再再再次打扰
      

  13.   

    INT我不知道怎么建索引,不过又改成
    set db=serve.createobject("adodb.connection")
    db.connectimeout=0
    db.commandtimeout=0
    db.open conn
    db.execute "exec p_calc"
    db.close
    这样的调用,还是超时~~郁闷
      

  14.   

    三万条数据,用ASP调用跑了半个小时了还没有执行完,是不是跑死徇环了呀?要不要在表里加个列来判断他是不是最后一级?高手帮呀
      

  15.   

    --下面是我的电脑上做的测试--测试表
    create table 表(
    dm int identity(1,1) primary key,  --主键
    name varchar(10),pid int,xf int,yj int)--在pid上建索引
    create index idx_pid on 表(pid)  
    数据规则,pid=0表示是最高级,而不是用pid=dm表示
      

  16.   

    --计算的存储过程做如下调整--调用存储过程进行计算
    exec p_calc
    go--为树形显示写函数
    create function f_id()
    returns @re table(id int primary key,sid varchar(8000),level int)
    as
    begin
    declare @l int
    set @l=0
    insert @re select dm,right(10000+dm,4),@l from 表 where pid=0
    while @@rowcount>0
    begin
    set @l=@l+1
    insert @re select a.dm,b.sid+right(10000+a.dm,4),@l
    from 表 a join @re b on a.pid=b.id and b.level=@l-1
    end
    return
    end
    go
      

  17.   

    看不明白。我把顶级的PID的值改为0了,你前面的存储过程应怎么改呢?
    create function f_id()
    returns @re table(id int primary key,sid varchar(8000),level int)
    as
    begin
    declare @l int
    set @l=0
    insert @re select dm,right(10000+dm,4),@l from 表 where pid=0
    while @@rowcount>0
    begin
    set @l=@l+1
    insert @re select a.dm,b.sid+right(10000+a.dm,4),@l
    from 表 a join @re b on a.pid=b.id and b.level=@l-1
    end
    return
    end
    go
    这里我看有ID主键,SID等列,我那里没有,我不明白你写的是什么意思。
    SORRY,在下愚昧~~