Asp经典打开另一条记录以防万一

时间:2013-08-15 13:21:53

标签: asp-classic

如果选择了案例7,我想显示所有这些记录,但我不知道如何在asp经典中打开specefied记录,是否可能?

    ELSE IF @Case = 7 BEGIN 
    SELECT TOP 5 * FROM dbo.tbl_PostalCode WHERE (PostalCode = @PostalCode)
    SELECT TOP 5 * FROM dbo.tbl_PostalCode_AutoWorkshop WHERE (PostalCode = @PostalCode)
    SELECT TOP 5 * FROM dbo.tbl_PostalCode_Expert WHERE (PostalCode = @PostalCode)
    SELECT TOP 5 * FROM dbo.tbl_PostalCode_Samsung WHERE (PostalCode = @PostalCode)
    SELECT TOP 5 * FROM dbo.tbl_PostalCode_SonyCIC WHERE (PostalCode = @PostalCode)
    SELECT TOP 5 * FROM dbo.tbl_PostalCode_Whitegoods WHERE (PostalCode = @PostalCode)
    END

这是html + asp我可以显示dbo.tbl_PostalCode的第一条记录

                <tr> 
                                  <td class="blackb">RecId</td>
                                    <td class="blackb">Country</td>
                                    <td class="blackb">PostalCode</td>
                                    <td class="blackb">Town</td>
                                    <td class="blackb">Lat</td>
                                    <td class="blackb">Lng</td>
                </tr>


                                                    <%
                                If Not RSOrder.EOF then
                                    Do Until RSOrder.EOF
                                %>
                                <tr>    
                                    <td><%=RSOrder("Rec_ID")%> </td>
                                        <td><%=RSOrder("Country")%></td>
                                        <td><%=RSOrder("PostalCode")%>  </td>
                                        <td><%=RSOrder("Town")%> </td>
                                        <td><%=RSOrder("Lat")%></td>
                                        <td><%=RSOrder("Lng")%></td>
                                </tr>


                                    <% 
                                        RSOrder.MoveNext
                                    Loop
                                Else%>




                                <%End If %>
                                <%End if%> 

1 个答案:

答案 0 :(得分:2)

你最好的办法是让你的SQL返回一个结果集而不是6,做这样的改变:

ELSE IF @Case = 7 
BEGIN 
    SELECT TOP 5 * FROM dbo.tbl_PostalCode WHERE (PostalCode = @PostalCode)
    UNION
    SELECT TOP 5 * FROM dbo.tbl_PostalCode_AutoWorkshop WHERE (PostalCode = @PostalCode)
    UNION
    SELECT TOP 5 * FROM dbo.tbl_PostalCode_Expert WHERE (PostalCode = @PostalCode)
    UNION
    SELECT TOP 5 * FROM dbo.tbl_PostalCode_Samsung WHERE (PostalCode = @PostalCode)
    UNION
    SELECT TOP 5 * FROM dbo.tbl_PostalCode_SonyCIC WHERE (PostalCode = @PostalCode)
    UNION
    SELECT TOP 5 * FROM dbo.tbl_PostalCode_Whitegoods WHERE (PostalCode = @PostalCode)
END

这种方式当您循环通过记录集时 - 单个循环将产生所有结果

作为替代方案,如果您需要保留当前返回多个结果的SQL,请使用Recodset的NextRecordset方法,它允许您创建一个循环,通过返回的结果循环(内部循环仍然是您当前的一个循环通过个人记录)

在你的情况下,它会像

Do Until RSOrder is Nothing

   Do Until RSOrder.EOF

      'Using function from http://www.devx.com/tips/Tip/42275 to see if specific field exists
      If FieldExistsInRS(RSOrder, "MyColumn")
         'do something specific for this result
      End If

      RSOrder.MoveNext
   Loop

   'Get result from the next SELECT
   Set RSOrder = RSOrder.NextRecordset 

Loop