SQL错误"' ='附近的语法不正确" - 经典的ASP / SQL服务器

时间:2014-07-11 14:29:13

标签: sql-server asp-classic

我在SQL Server数据库上运行以下SQL查询(通过MS Access和ODBC)

SELECT DISTINCT 
   D.DOC_TYPE, D.DOC_REF, 
   COUNT(D.GUID) AS Total, 
   SUM(IIF(I.TEAM = 'OPS1', 1, 0)) AS OPS1, 
   SUM(IIF(I.TEAM = 'OPS2', 1, 0)) AS OPS2, 
   SUM(IIF(I.TEAM = 'OPS3', 1, 0)) AS OPS3, 
   SUM(IIF(I.TEAM = 'OPS4', 1, 0)) AS OPS4, 
   SUM(IIF(I.TEAM = 'OPS5', 1, 0)) AS OPS5, 
   SUM(IIF(I.TEAM = 'OPS6', 1, 0)) AS OPS6, 
   SUM(IIF(I.TEAM = 'OPS7', 1, 0)) AS OPS7, 
   SUM(IIF(I.TEAM = 'OPS8', 1, 0)) AS OPS8, 
   SUM(IIF(I.TEAM = 'OPS9', 1, 0)) AS OPS9, 
   SUM(IIF(I.TEAM = 'OPS10', 1, 0)) AS OPS10, 
   SUM(IIF(I.TEAM Not Like 'OPS*', 1, 0)) AS OtherTeams
FROM 
   dbo_DOCUMENT AS D 
INNER JOIN 
   dbo_INTRAY AS I ON I.DOC_GUID = D.GUID
WHERE 
   D.DOC_TYPE like 'GIM*'
   AND I.FILE_SYSTEM = 'B' 
   AND I.MAIL_STATUS IN ('N', 'NT')
GROUP BY 
   D.DOC_TYPE, D.DOC_REF;

运行正常。我已将其粘贴到经典ASP页面变量中,如下所示

SQL =   "SELECT DISTINCT D.DOC_TYPE, D.DOC_REF, COUNT(D.GUID) AS Total, " & _
        "SUM(IIF(I.TEAM='OPS1',1,0)) AS OPS1, " & _
        "SUM(IIF(I.TEAM='OPS2',1,0)) AS OPS2, " & _
        "SUM(IIF(I.TEAM='OPS3',1,0)) AS OPS3, " & _
        "SUM(IIF(I.TEAM='OPS4',1,0)) AS OPS4, " & _
        "SUM(IIF(I.TEAM='OPS5',1,0)) AS OPS5, " & _
        "SUM(IIF(I.TEAM='OPS6',1,0)) AS OPS6, " & _
        "SUM(IIF(I.TEAM='OPS7',1,0)) AS OPS7, " & _
        "SUM(IIF(I.TEAM='OPS8',1,0)) AS OPS8, " & _
        "SUM(IIF(I.TEAM='OPS9',1,0)) AS OPS9, " & _
        "SUM(IIF(I.TEAM='OPS10',1,0)) AS OPS10, " & _
        "SUM(IIF(I.TEAM Not Like 'OPS*',1,0)) AS OtherTeams " & _
        "FROM DOCUMENT AS D INNER JOIN INTRAY AS I ON I.DOC_GUID = D.GUID " & _
        "WHERE D.DOC_TYPE like 'GIM*' " & _
        "AND I.FILE_SYSTEM='B' AND I.MAIL_STATUS IN ('N', 'NT') " & _
        "GROUP BY D.DOC_TYPE, D.DOC_REF;" 

但是,在ASP页面中运行时,它会报告以下错误;

  

Microsoft OLE DB Provider for SQL Server错误“80040e14”

     

'='附近的语法不正确。

     

/bactu/IWStats/GIM1.asp,第49行

第49行是记录集的打开位置。

任何线索?我甚至尝试将Response.Write(SQL)放入输出页面并将SQL从那里复制到Access中,它运行正常。

1 个答案:

答案 0 :(得分:0)

您正在使用MSAccess查询语法,但您的错误将返回SQL Server错误。请尝试使用TSQL语法。试试这个:

SQL = "SELECT DISTINCT D.DOC_TYPE, D.DOC_REF, COUNT(D.GUID) AS Total, " & _
    "SUM(CASE WHEN I.TEAM='OPS1' THEN 1 ELSE 0 END) AS OPS1, " & _
    "SUM(CASE WHEN I.TEAM='OPS2' THEN 1 ELSE 0 END) AS OPS2, " & _
    "SUM(CASE WHEN I.TEAM='OPS3' THEN 1 ELSE 0 END) AS OPS3, " & _
    "SUM(CASE WHEN I.TEAM='OPS4' THEN 1 ELSE 0 END) AS OPS4, " & _
    "SUM(CASE WHEN I.TEAM='OPS5' THEN 1 ELSE 0 END) AS OPS5, " & _
    "SUM(CASE WHEN I.TEAM='OPS6' THEN 1 ELSE 0 END) AS OPS6, " & _
    "SUM(CASE WHEN I.TEAM='OPS7' THEN 1 ELSE 0 END) AS OPS7, " & _
    "SUM(CASE WHEN I.TEAM='OPS8' THEN 1 ELSE 0 END) AS OPS8, " & _
    "SUM(CASE WHEN I.TEAM='OPS9' THEN 1 ELSE 0 END) AS OPS9, " & _
    "SUM(CASE WHEN I.TEAM='OPS10' THEN 1 ELSE 0 END) AS OPS10, " & _
    "SUM(CASE WHEN I.TEAM Not Like 'OPS%' THEN 1 ELSE 0 END) AS OtherTeams " & _
    "FROM DOCUMENT AS D INNER JOIN INTRAY AS I ON I.DOC_GUID = D.GUID " & _
    "WHERE D.DOC_TYPE like 'GIM%' " & _
    "AND I.FILE_SYSTEM='B' AND I.MAIL_STATUS IN ('N', 'NT') " & _
    "GROUP BY D.DOC_TYPE, D.DOC_REF;" 

CASE是TSQL等同于IIF,TSQL使用%作为LIKE的通配符而不是*。