为什么我不能在一个查询中从3个不同的表中得到两个以上的值

时间:2010-03-23 04:35:33

标签: sql-server-2005 asp-classic

这很奇怪。在新闻详细信息页面中,我想通过一个查询从不同的表中获取一些不同的值。但是,由于一些奇怪的原因,我只能得到两个值。结果如下:

<Desc></Desc>
<Date/>
</row>
</rows>

如果我禁用全名,那么我会获得shortdesc而不是其他人。其他人也一样。

NewsID = Request.QueryString("NEWSID")  

SQL = "SELECT N.NewsID, N.MembersID, N.CategoriesID, N.ImagesID, N.NewsTitle, N.NewsShortDesc, N.NewsDesc, N.NewsActive, N.NewsDateEntered, C.CategoriesID, C.CategoriesName, M.MembersID, M.MembersFullName"

Set objViewNews = objConn.Execute(SQL)  

With Response
    .Write "<?xml version='1.0' encoding='windows-1254' ?>"
    .Write "<rows>"
End With

With Response
    .Write "<row id='"& objViewNews("NewsID") &"'>"
    .Write "<FullName>"& objViewNews("MembersFullName") &"</FullName>"
    .Write "<CategoryName>"& objViewNews("CategoriesName") &"</CategoryName>"
    .Write "</row>"
End With

With Response
    .Write "</rows>"
End With    

objViewNews.Close
Set objViewNews = Nothing

1 个答案:

答案 0 :(得分:0)

  1. 我假设您期待单排或只关心第一排?
  2. MembersFullName可以为空吗? NewsShortDesc是否可以为空?
  3. 您应该始终检查输入,而不是将它们直接连接到字符串中。因此,您应该使用一个函数来验证和检查NewsID是否符合您期望的类型并正确转义,如下所示:
  4. SQL = SQL & " Where N.NewsId = " & SqlString("int", newsId)

    4.您可以考虑使用JOIN语法:

    (假定输入检查功能)

        SQL = "SELECT N.NewsID, N.MembersID, N.CategoriesID, N.ImagesID, N.NewsTitle, N.NewsShortDesc, N.NewsDesc, N.NewsActive, N.NewsDateEntered, C.CategoriesID, C.CategoriesName, M.MembersID, M.MembersFullName"
        SQL = SQL & " FROM News N"
        SQL = SQL & "   Inner Join Categories C"
        SQL = SQL & "       On C.CategoriesID = N.CategoriesID"
        SQL = SQL & "   Inner Join Members M"
        SQL = SQL & "       On M.MembersID = N.MembersID"
        SQL = SQL & " Where N.NewsId = " & SqlString(VariantType.Integer
    , NewsId)
        SQL = SQL & "   And N.NewsActive = 1"
    

    (男人的格式可能很古怪)

    编辑您应该尝试将所有值包装在CDATA中,如下所示:

    With Response
        .Write "<row id='"& objViewNews("NewsID") &"'>"
        .Write "<FullName><![CDATA["& objViewNews("MembersFullName") &"]]></FullName>"
        .Write "<CategoryName><![CDATA["& objViewNews("CategoriesName") &"]]></CategoryName>"
        .Write "<Title><![CDATA["& objViewNews("NewsTitle") &"]]></Title>"
        .Write "<ShortDesc><![CDATA["& objViewNews("NewsShortDesc") &"]]></ShortDesc>"
        .Write "<Desc><![CDATA["& objViewNews("NewsDesc") &"]]></Desc>"
        .Write "<Date>"& objViewNews("NewsDateEntered") &"</Date>"
        .Write "</row>"
    End With
    

    我没有将日期值包装在CDATA中,尽管您也可以考虑这样做以确定。