下标超出范围:执行存储过程后执行'0'(asp-classic)

时间:2017-03-17 13:26:50

标签: sql-server stored-procedures asp-classic

我有一个这样的存储过程:

USE [RK]
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER PROCEDURE [dbo].[PRC_THEKE_FRAGE_KOMPLETTIERUNG_SELECT] @sgi nvarchar(8), @is_nll bit
AS

if @is_nll = 1

select distinct (s.Ort + ' ' + s.Standort + ' ' + s.Strasse) as place
, ROUND(cast(sum(sort.fragenstatus) as real)/cast(sum(sort1.FragenStatus) as real) *100,0) as prozent
from Standorte s
left join LLThekeStamm stamm on s.S_ID = stamm.S_ID
left join LLFragenStandort sort on stamm.LLT_ID = sort.LLT_ID
left join LLThekeFragen tf on sort.FragenId = tf.FragenID
left join LLFragenStandort sort1 on stamm.LLT_ID = sort.LLT_ID and sort1.FragenStatus = 1
where ISNULL(tf.Aktiv, '') = 1 AND s.NLL_SGI = @sgi
group by (s.Ort + ' ' + s.Standort + ' ' + s.Strasse)

else

select distinct (s.Ort + ' ' + s.Standort + ' ' + s.Strasse) as place
, ROUND(cast(sum(sort.fragenstatus) as real)/cast(sum(sort1.FragenStatus) as real) *100,0) as prozent
from Standorte s
left join LLThekeStamm stamm on s.S_ID = stamm.S_ID
left join LLFragenStandort sort on stamm.LLT_ID = sort.LLT_ID
left join LLThekeFragen tf on sort.FragenId = tf.FragenID
left join LLFragenStandort sort1 on stamm.LLT_ID = sort.LLT_ID and sort1.FragenStatus = 1
where ISNULL(tf.Aktiv, '') = 1
group by (s.Ort + ' ' + s.Standort + ' ' + s.Strasse)

只是为了澄清:它might甚至不关心这个SP的输出是什么,但只是为了表明有输出,我会为你附上一些东西:

enter image description here

如您所见,设置了正确的别名(名称placeprozent)。

在我的ASP-classic部分,我正在召唤这个SP。看看这个:

sub getLocationPercentages()
'open DB connection
conn.open "something"

dim array_prozente()

'counter-variable 
p1 = 0

SQL = "EXEC PRC_THEKE_FRAGE_KOMPLETTIERUNG_SELECT '" & session("sgi") & "',1"

set rs2 = CreateObject("ADODB.Recordset")
rs2.open SQL,conn,1,3
redim array_prozente(rs2.RecordCount,1)

'fill array with expected values
do while not rs2.EOF
    array_prozente(p1, 0) = rs2("place") ' <-- this is where the error points at
    array_prozente(p1, 1) = rs2("prozent")
    p1 = p1 + 1
    rs2.MoveNext
loop

rs2.close()
conn.close()

end sub

这最终出现在以下错误消息中:

  

下标超出范围:'0'

然后打印SQL-String会显示正确的语法。将此输出复制到我的查询窗口会导致显示正确的值。

在我看来,我试图像这样访问它:

<% for i = 0 to p1 %>
    <% if array_prozente(i, 0) > "" then %>
        <tr><td><%= array_prozente(i, 0) %></td><td><%= array_prozente(i, 1) %></td></tr>
    <% end if %>
<% next %>

我做错了什么?

简短陈述

简单的SQL查询按预期工作,即使使用recordcount也是如此。这只是在使用SP时发生(现在)。

2 个答案:

答案 0 :(得分:1)

不是ASP方面的答案,但是你的程序有多个执行路径,由于计​​划缓存可能导致一些严重的性能问题。在这种情况下,优点是您可以大规模简化您的程序并获得一致的性能。以下是修改程序以适应两种路径的方法。

ALTER PROCEDURE [dbo].[PRC_THEKE_FRAGE_KOMPLETTIERUNG_SELECT] @sgi nvarchar(8), @is_nll bit
AS

select distinct (s.Ort + ' ' + s.Standort + ' ' + s.Strasse) as place
, ROUND(cast(sum(sort.fragenstatus) as real)/cast(sum(sort1.FragenStatus) as real) *100,0) as prozent
from Standorte s
left join LLThekeStamm stamm on s.S_ID = stamm.S_ID
left join LLFragenStandort sort on stamm.LLT_ID = sort.LLT_ID
left join LLThekeFragen tf on sort.FragenId = tf.FragenID
left join LLFragenStandort sort1 on stamm.LLT_ID = sort.LLT_ID and sort1.FragenStatus = 1
where ISNULL(tf.Aktiv, '') = 1 AND s.NLL_SGI = case when @sgi = 1 then @sgi else s.NLL_SGI end
group by (s.Ort + ' ' + s.Standort + ' ' + s.Strasse)

答案 1 :(得分:0)

使用GetRows(),因为它比你在第一个子目录中的效率更高。

sub getLocationPercentages()
    conn.open "something"

    SQL = "EXEC PRC_THEKE_FRAGE_KOMPLETTIERUNG_SELECT '" & session("sgi") & "',1"

    set rs2 = CreateObject("ADODB.Recordset")
    rs2.open SQL,conn,1,3

    if not rs2.eof then
        array_prozente = rs2.getRows()    
        rs2.close()

        if isArray(array_prozente) then
            for i = 0 to ubound(array_prozente, 2)
                response.write "<tr><td>"
                response.write array_prozente(0,i)
                response.write "</td><td>"
                response.write array_prozente(1,i)
                response.write "</td></tr>"
            next
        end if
    else
        err.raise 8, "mypage.asp", "Query did not return results."
    end if

    conn.close()    
end sub