(皱健)进,超难的存储过程问题
以下为ASP计算程序,数据库内关联表为 award,user1 其中award与user1表中的dm对应。USER1为用户数据库,PID字段对应DM字段,为下级。这里会有几百级。CENG说明他为多少层会员。AWARD为奖金数据库,每次计算后CI加1,计算奖金的方法有七种,七种奖金计算累计为一期的奖金。
以下为七种奖金计算的程序,但因为数据量大,并且要对两个表进行多次递归及循环,所以常造成超时。想改为用存储过程计算,却不知如何改。请各位大侠帮忙!
<%
set rs1=server.CreateObject("adodb.recordset")
sql1="select * from award where sdate=(select max(sdate) from award)" '取出奖金数据库中日期最大的一条记录
rs1.open sql1,conn,1,1
if rs1.eof and rs1.bof then
qdate=date() '如果没有记录则取当前日期
ci=0
else
if rs1("sdate")=>date then
response.Write "<SCRIPT language=JavaScript>alert('今天刚计算过奖励金额!!');"
response.write"javascript:history.go(-1)</SCRIPT>"
response.end '如果最大日期大于当前日期,则提示。
else
qdate=dateadd("d",1,rs1("sdate")) '如果最大日期小于当前日期,则取最大日期后一天。
end if
ci=cint(rs1("ci"))
end if
rs1.close()
set rs1=nothing
set rs=server.CreateObject("adodb.recordset")
sql="select * from user1"
rs.open sql,conn,1,1 '打开用户表,以便计算用户各奖金额项
if rs.eof and rs.bof then
rs.close()
set rs=nothing '如果用户表没有数据,则关闭RS对象
else
while not rs.eof '用户表徇环进入计算
'龙脉第一项奖金值提取开始
if rs("zrs")>=10 and rs("yrs")>=10 then
i=cint(len(rs("zrs"))-1)
y=cint(len(rs("yrs"))-1)
ii=cint(left(rs("zrs"),i)&"0")
yy=cint(left(rs("yrs"),y)&"0")
if rs("zrs")>rs("yrs") then
jiang1=yy*20
zrs=rs("zrs")-yy
yrs=rs("yrs")-yy
elseif rs("zrs")<=rs("yrs") then
jiang1=ii*20
zrs=cint(rs("zrs"))-ii
yrs=cint(rs("yrs"))-ii
end if
elseif rs("zrs")<10 and rs("yrs")<10 then
jiang1=0
zrs=rs("zrs")
yrs=rs("yrs")
end if
'龙脉第一项奖金值提取结束,第二项奖金值提取开始
jiang2=cint(rs("ztj"))
'龙脉第二项奖金值提取结束,第三项奖金值提取开始
if rs("lei")=2 then
if rs("zjf")>=400 and rs("yjf")>=400 then
zjf=rs("zjf")/400
yjf=rs("yjf")/400
if zjf<yjf then
jfjj=zjf
elseif zjf>yjf then
jfjj=yjf
elseif zjf=yjf then
jfjj=zjf
end if
jiang3=jfjj*60
zyjf=jfjj*400
if jiang3>5000 then
jiang3=5000
end if
else
jfjj=0
jiang3=0
zyjf=0
end if
elseif rs("lei")=3 then
if rs("zjf")>=800 and rs("yjf")>=800 then
zjf=rs("zjf")/800
yjf=rs("yjf")/800
if zjf<yjf then
jfjj=zjf
elseif zjf>yjf then
jfjj=yjf
elseif zjf=yjf then
jfjj=zjf
end if
jiang3=jfjj*120
zyjf=jfjj*800
if jiang3>30000 then
jiang3=30000
end if
else
jfjj=0
jiang3=0
zyjf=0
end if
else
jfjj=0
jiang3=0
zyjf=0
end if
'龙脉第三项奖金值提取结束,第四项奖金在二次计算中完成,进入第六项奖金值提取。
jinag6=rs("jiang6")
if rs("lei")<=1 then '如果会员为第一档,则只计算第一项奖金额
sqltuser="update user1 set zrs='"&zrs&"',yrs='"&yrs&"' where dm="&rs("dm")
conn.execute sqltuser
sqltext="insert into award (qdate,sdate,dm,zmdm,jiang1,ci) values('"&qdate&"','"&date&"','"&rs("dm")&"','"&rs("zm")&"','"&jiang1&"','"&ci+1&"')"
conn.execute sqltext
else
sqltuser="update user1 set zrs='"&zrs&"',yrs='"&yrs&"',ztj=0,zjf=zjf-"&zyjf&",yjf=yjf-"&zyjf&",jiang6=0 where dm="&rs("dm")
conn.execute sqltuser
sqltext="insert into award (qdate,sdate,dm,zmdm,jiang1,jiang2,jiang3,jiang6,ci,ceng) values('"&qdate&"','"&date&"','"&rs("dm")&"','"&rs("zm")&"','"&jiang1&"','"&jiang2&"','"&jiang3&"','"&jiang6&"','"&ci+1&"','"&rs("ceng")&"')"
conn.execute sqltext
end if
rs.movenext
wend '徇环结束,计算结束
rs.close()
set rs=nothing
end if
'进入第五项奖金计算
set rs3=server.CreateObject("adodb.recordset")
sql3="select * from user1" '查询所有用户
rs3.open sql3,conn,1,1
if rs3.eof and rs3.bof then
rs3.close()
set rs3=nothing'如没有用户则计算结束
else
while not rs3.eof '徇环所有用户
if rs3("lei")<3 then '如果用户档位不为第三档则跳过
else
set rs=server.CreateObject("adodb.recordset")
sql="select * from user1 where pid="&rs3("dm")&" order by id" '正排序,ID小在前
rs.open sql,conn,1,1
if rs.eof and rs.bof then
rs.close()
set rs=nothing
else
if rs.recordcount<=1 then
jiangduan="jiang51" '如果记录只有一条,则为左线
call uuu(rs("dm"))
else
i=0 '设置变量I
while not rs.eof
i=i+1
jiangduan="jiang5"&i&"" '两条以上记录时,随时徇环而更改字段名以判断为左线还是右线。
call uuu(rs("dm"))
rs.movenext
wend
end if
end if
end if
rs3.movenext
wend
rs3.close()
set rs3=nothing
end if