查询在Access中运行但在C#DataSet中生成错误

时间:2011-12-19 15:05:48

标签: sql c#-4.0 xsd ms-access-2007

我有这个查询,当我在Access中测试它时运行正常,但是我在外部C#程序中运行它来自TSQL <老实说,我还在学习条款所以我认为TSQL对我正在做的事情是正确的(事实并非如此))。当我尝试将该过程存储在我的XSD文件中的tableadapter中时,我收到一个错误(请参阅下面的查询)。因此,不会自动创建正确的XML架构。虽然查询仍然运行,但它也会出错。我希望它能在没有错误的情况下运行,但无法弄清楚错误的真正位置。我尝试通过用CASE语句替换IIF来消除所有“=”,但后来我遇到了其他错误(如果有帮助我可以发布该尝试)

SELECT  IIF(ISNULL(Owners.OwnFirstName), Owners.OwnLastName, 
         Owners.OwnFirstName + ' ' + Owners.OwnLastName) AS [Owner's Name], SUM(iif(UnitBarcodes.NephStatus = 'N', 1, 0)) 
         AS [Neph units], SUM(iif(UnitBarcodes.NephStatus = 'F', 1, 0)) AS [Filter units], COUNT(UnitBarcodes.NephStatus) 
         AS [Total Units]
FROM  (Owners INNER JOIN
         ((UnitBarcodes INNER JOIN
         AssembledUnits ON UnitBarcodes.ID = AssembledUnits.CaseID) INNER JOIN
         OwnerUnits ON AssembledUnits.ID = OwnerUnits.AssembledUnitID) ON Owners.ID = OwnerUnits.OwnerID)
GROUP BY IIF(ISNULL(Owners.OwnFirstName), Owners.OwnLastName, 
         Owners.OwnFirstName + ' ' + Owners.OwnLastName)

来自查询生成器的错误消息

Error in list of function arguments: '=' not recognized.
Unable to parse query text.

无论如何,谢谢你的帮助!

修改

这是我之前用CASE取代IIF的尝试

SELECT   (case when ISNULL(Owners.OwnFirstName) then Owners.OwnLastName else (Owners.OwnFirstName + ' ' + Owners.OwnLastName) end) AS [Owner's name],
          SUM(case where UnitBarcodes.NephStatus = 'N' then  1 else 0 end)) 
          AS [Neph units], SUM( case when UnitBarcodes.NephStatus = 'F' then 1 else 0 end)) AS [Filter units], COUNT(UnitBarcodes.NephStatus) 
          AS [Total units]
FROM         (Owners inner JOIN
          ((UnitBarcodes inner JOIN
          AssembledUnits ON UnitBarcodes.ID = AssembledUnits.CaseID) INNER JOIN
          OwnerUnits ON AssembledUnits.ID = OwnerUnits.AssembledUnitID) ON Owners.ID = OwnerUnits.OwnerID)
GROUP BY (case when ISNULL(Owners.OwnFirstName) then Owners.OwnLastName else (Owners.OwnFirstName + ' ' + Owners.OwnLastName) end) AS [Owner's name]

我得到的错误是

Error in list of function arguments: 'ISNULL' not recognized.
Error in list of function arguments: ')' not recognized.
Unable to parse query text.

EDIT2:

所以这个查询工作得很好

SELECT     IIF(ISNULL(Owners.OwnFirstName), Owners.OwnLastName, 
            Owners.OwnFirstName + ' ' + Owners.OwnLastName) AS [Owner's Name], COUNT(UnitBarcodes.NephStatus) 
            AS [Total units]
FROM         (Owners INNER JOIN
            ((UnitBarcodes INNER JOIN
            AssembledUnits ON UnitBarcodes.ID = AssembledUnits.CaseID) INNER JOIN
            OwnerUnits ON AssembledUnits.ID = OwnerUnits.AssembledUnitID) ON Owners.ID = OwnerUnits.OwnerID)
GROUP BY IIF(ISNULL(Owners.OwnFirstName), Owners.OwnLastName, 
            Owners.OwnFirstName + ' ' + Owners.OwnLastName)

但是只要我添加任何Sum(通过CASE或IIF),查询就会生成错误。

3 个答案:

答案 0 :(得分:1)

您可以使用COALESCE将NULL列替换为空字符串,并使用CASE替换SUM中的IIF。

SELECT  COALESCE(Owners.OwnFirstName + ' ','') + Owners.OwnLastName AS [Owner's Name],  
        SUM(CASE WHEN UnitBarcodes.NephStatus = 'N' THEN 1 ELSE 0 END) AS [Neph units], 
        SUM(CASE WEHN UnitBarcodes.NephStatus = 'F' THEN 1 ELSE 0 END) AS [Filter units], 
        COUNT(UnitBarcodes.NephStatus) AS [Total Units]
FROM  (Owners INNER JOIN
         ((UnitBarcodes INNER JOIN
         AssembledUnits ON UnitBarcodes.ID = AssembledUnits.CaseID) INNER JOIN
         OwnerUnits ON AssembledUnits.ID = OwnerUnits.AssembledUnitID) ON Owners.ID = OwnerUnits.OwnerID)
GROUP BY COALESCE(Owners.OwnFirstName + ' ','') + Owners.OwnLastName

答案 1 :(得分:0)

您需要将IIF语句转换为CASE语句,IIF不是有效的T-SQL语法。

在回复你的评论时,这不是如何在t-sql中进行空检查,这是一个例子:

CASE
     WHEN SomeValue IS NULL THEN
          SomeDefault
     ELSE
          SomeValue 

END

答案 2 :(得分:0)

很多语法错误。

我格式化了您的最后一次尝试并更正了一些错误。希望这对你有用。

SELECT  case    when Owners.OwnFirstName IS NULL 
                then Owners.OwnLastName 
                else Owners.OwnFirstName + ' ' + Owners.OwnLastName 
        end AS [Owner's name]
        ,SUM(case when UnitBarcodes.NephStatus = 'N' then 1 else 0 end) AS [Neph units]
        ,SUM(case when UnitBarcodes.NephStatus = 'F' then 1 else 0 end) AS [Filter units]
        ,COUNT(UnitBarcodes.NephStatus) AS [Total units]
FROM    Owners 
JOIN    OwnerUnits
    ON  Owners.ID = OwnerUnits.OwnerID
JOIN    AssembledUnits
    ON  AssembledUnits.ID = OwnerUnits.AssembledUnitID
JOIN    UnitBarcodes 
    ON  UnitBarcodes.ID = AssembledUnits.CaseID
GROUP BY 
    case    when Owners.OwnFirstName IS NULL 
            then Owners.OwnLastName 
            else Owners.OwnFirstName + ' ' + Owners.OwnLastName 
    end