如何使用vbscript对列的值求和

时间:2012-06-04 05:45:36

标签: asp-classic vbscript ado

我正在尝试获取列的值的总和,因为我在select语句中使用了SUM()。

<%
            sql = "select SUM(OpcCalEstQuantity) as qt_total from [Sheet1$] where cInt(JobEstimateNumber) = '"&cint(request.QueryString("no"))&"' and MccDescription = 'CTP Plate Making & Plates' and MaoOperationDescription = 'Plate Making'"
            rs1.open sql, con, 1, 2
            do while not rs1.eof
            %>
            <td style="padding:3px; text-align:right;"><%=rs1("qt_total")%></td>
            <%
                rs1.movenext
                loop
                rs1.close
            %>

但在浏览器上显示时会出现此错误。

Microsoft JET Database Engine error '80040e14'

Invalid use of Null 

所以我认为解决方法是使用vbscript来计算值。但是没有这样的函数来计算列中的值。

2 个答案:

答案 0 :(得分:1)

我不是很喜欢SQL和MS Jet引擎,但我认为你想要SUM的列包含一些NULL值。要摆脱它们,如果您的数据库支持它,您可以使用coalesce函数,如:

sql = "select SUM(COALESCE(OpcCalEstQuantity, 0)) as qt_total from ......"

答案 1 :(得分:1)

如果您想在SQL中解决这个问题,那么合并是一个很好的建议 如果你想纯粹在vbscript / asp中解决它,你必须自己循环并计算总金额,试试这个:

<%
    sql = "select OpcCalEstQuantity from [Sheet1$] where cInt(JobEstimateNumber) = '"&cint(request.QueryString("no"))&"' and MccDescription = 'CTP Plate Making & Plates' and MaoOperationDescription = 'Plate Making'"
    rs1.open sql, con, 1, 2
%>

<%  dim total : total = 0
    do while not rs1.eof 
        if NOT(isNull(rs1("OpcCalEstQuantity")) OR rs1("OpcCalEstQuantity")="") then total = total + cDbl(rs1("OpcCalEstQuantity"))
        rs1.movenext
    loop
    rs1.close
%>
<td style="padding:3px; text-align:right;"><%=total%></td>

希望这有帮助,
埃里克

相关问题