我是搞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,如有困难,愿出人民币求助~
具体是这样的。表 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,如有困难,愿出人民币求助~
解决方案 »
- 想问问各位DBA一个问题
- 非常急,在线等,问一个比较简单的问题,思路不清楚,急~~~~~~~~~~~~
- 简单问题(文件组)
- 存图片的相对路径,数据类型应该是什么?
- 急啊! SQL中的数据如何与orcale数据库同步
- 怎样将这个存储过程的参数可以动态控制(多一个参数或少一个参数都能运行)
- 怎样将excel中的数据转到ms-sql中
- 寻求CD-KEY
- 为什么重新安装sql server数据库后在连接以前的数据库不让连接,给出以下提示!
- 如何设计一个超大容量的数据库,我用的是SQL Server 2000
- 我手头一个项目想外包出去,有兴趣的请联系我!
- 设计数据库:设计表时如何才能避免"导致循环级联"
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
在作业里面执行上面的语句,我想应该可以吧,我也是一个菜鸟啊,
有机会的话明天确认了再跟你留言啊~~
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
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 行)
--*/
conn="数据库连接字符串"
set db=server.createobject("adodb.connection")
db.open conn
db.execute "exec p_calc"
db.close
%>
<%
conn="数据库连接字符串"
set db=serve.createobject("adodb.connection")
db.connectimeout=0
db.commandtimeout=0
db.open conn
db.execute "exec p_calc"
db.close
%>
执行后出错提示~~再次打扰请教
set db=serve.createobject("adodb.connection")
db.connectimeout=0
db.commandtimeout=0
db.open conn
db.execute "exec p_calc"
db.close
这样的调用,还是超时~~郁闷
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表示
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
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,在下愚昧~~