sql循环更新记录问题(两个表有关联的)
请看以下代码
<%
set conn=opendb( "oabusy ", "conn ", "accessdsn ")
'打开护品名库
sql= "select c_hpname,n_count from hplb where n_count=1 "
rs.open sql,conn,1
while not rs.eof and not rs.bof
hpname=rs( "c_hpname ")
sql= "select sum(n_lqsl) as kk from hpgl_bak where c_hpname= " & sqlstr(hpname)
set rs=conn.execute(sql)
mm=rs( "kk ")
sql = "Update hplb set n_count= " & sqlstr(mm) & " where c_hpname= " & sqlstr(hpname)
conn.Execute sql
rs.movenext
wend
%>
我意为将明细表hpgl_bak(n_lqsl列)的值分类汇总后更新类别表hplb(n_count列)的
值,但是程序运行结果只是将表hplb(n_count列)最前符合条件的一条记录修改了,
其他符合条件的记录未更新。请教各位老师,上述循环语句如何修改?
[解决办法]
sql= "select sum(n_lqsl) as kk from hpgl_bak where c_hpname= " & sqlstr(hpname)
set rs=conn.execute(sql)
mm=rs( "kk ")
把上面这个改成
sql= "select sum(n_lqsl) as kk from hpgl_bak where c_hpname= " & sqlstr(hpname)
set rs2 = server.createobject( "adodb.recordset ")
set rs2=conn.execute(sql)
mm=rs2( "kk ")
试试,你之前的问题是后一个 rs 把外层循环的 rs 给覆盖了
如果可以了,记得给分~