关键字'SELECT'附近的语法不正确

时间:2009-09-23 14:20:28

标签: sql asp-classic

我有一个经典ASP页面,可让用户搜索属性和结果页面,然后通过电子邮件发送具有匹配属性的属性代理,让他们知道有租户对其属性感兴趣。

某些代理商可能拥有超过1个房产,在这种情况下,代理商只需要收到1封电子邮件,而不是每封房产的电子邮件。

我在Management Studio中测试了以下查询,它返回了正确的结果;

SELECT DISTINCT CustomerEmail
FROM         (SELECT     ContentID
                       FROM          (SELECT     ContentID
                                               FROM          VWTenantPropertiesResults
                                               WHERE      (ContentStreet = '')
                                               UNION ALL
                                               SELECT     ContentID
                                               FROM         VWTenantPropertiesResults AS VWTenantPropertiesResults_2
                                               WHERE     (ContentTown = 'Hull')
                                               UNION ALL
                                               SELECT     ContentID
                                               FROM         VWTenantPropertiesResults AS VWTenantPropertiesResults_1
                                               WHERE     (ContentPostCode = 'HU7')) AS qi
                       GROUP BY ContentID
                       HAVING      (COUNT(*) >= 2)) AS q INNER JOIN
                      VWTenantPropertiesResults AS r ON r.ContentID = q.ContentID
WHERE     (r.ContentBedRooms BETWEEN 1 AND 4) AND (r.ContentPrice BETWEEN 50 AND 500)

但是,当我在页面中运行以下代码时,它会在关键字“SELECT”附近返回错误语法错误;

rsemailagents.Source = "SELECT DISTINCT CustomerEmail"
rsemailagents.Source = rsemailagents.Source& "FROM    ("
rsemailagents.Source = rsemailagents.Source& "        SELECT  ContentID"
rsemailagents.Source = rsemailagents.Source& "        FROM    ("
rsemailagents.Source = rsemailagents.Source& "                SELECT  ContentID"
rsemailagents.Source = rsemailagents.Source& "                FROM    VWTenantPropertiesResults"
rsemailagents.Source = rsemailagents.Source& "                WHERE   ContentStreet = '" & Replace(rspropertyresults__varReqStreet, "'", "''") & "'"
rsemailagents.Source = rsemailagents.Source& "                UNION ALL"
rsemailagents.Source = rsemailagents.Source& "                SELECT  ContentID"
rsemailagents.Source = rsemailagents.Source& "                FROM    VWTenantPropertiesResults"
rsemailagents.Source = rsemailagents.Source& "                WHERE   ContentTown = '" & Replace(rspropertyresults__varReqTown, "'", "''") & "'"
rsemailagents.Source = rsemailagents.Source& "                UNION ALL"
rsemailagents.Source = rsemailagents.Source& "                SELECT  ContentID"
rsemailagents.Source = rsemailagents.Source& "                FROM    VWTenantPropertiesResults"
rsemailagents.Source = rsemailagents.Source& "                WHERE   ContentTrimmedPostCode LIKE '" & Replace(varPostcode, "'", "''") & "%'"
rsemailagents.Source = rsemailagents.Source& "                ) qi"
rsemailagents.Source = rsemailagents.Source& "        GROUP BY"
rsemailagents.Source = rsemailagents.Source& "                ContentID"
rsemailagents.Source = rsemailagents.Source& "        HAVING  COUNT(*) >= 2"
rsemailagents.Source = rsemailagents.Source& "        ) q "
rsemailagents.Source = rsemailagents.Source& "JOIN    VWTenantPropertiesResults r "
rsemailagents.Source = rsemailagents.Source& "ON      r.ContentID = q.ContentID "
rsemailagents.Source = rsemailagents.Source& "WHERE   ContentBedrooms BETWEEN " & Replace(rspropertyresults__varBedroomsNoMin, "'", "''") & " "
rsemailagents.Source = rsemailagents.Source& "AND     " & Replace(rspropertyresults__varBedroomsNoMax, "'", "''") & " "
rsemailagents.Source = rsemailagents.Source& "AND     ContentPrice BETWEEN " & Replace(rspropertyresults__varPriceMin, "'", "''") & " "
rsemailagents.Source = rsemailagents.Source& "AND     " & Replace(rspropertyresults__varPriceMax, "'", "''") & " " & varSQL & " "

我想知道是否有人对DISTINCT在代码中不起作用的原因有任何想法?

谢谢。

4 个答案:

答案 0 :(得分:5)

在此行末尾的字符串中放置一个空格

更改

rsemailagents.Source = "SELECT DISTINCT CustomerEmail"

到此

rsemailagents.Source = "SELECT DISTINCT CustomerEmail "

答案 1 :(得分:2)

rsemailagents.Source = "SELECT DISTINCT CustomerEmail"

客户电子邮件背后需要有空格。

rsemailagents.Source = "SELECT DISTINCT CustomerEmail "

答案 2 :(得分:2)

我没有阅读所有内容,但在第一行的末尾,空格不足: - )

我发现在每行的开头添加空格更容易,因为您可以很容易地看到它是否丢失。当然,它仍然是丑陋的代码,就像将sql语句放在一起一样。

答案 3 :(得分:0)

您显示的SQL代码与ASP代码呈现的内容之间存在差异。您应该从ASP页面输出代码以查看正在进行的操作。

在%符号之前的LIKE语句中缺少单引号。