Query runs in Access but generates error in C# DataSet

让人想犯罪 __ 提交于 2019-12-11 14:16:49

问题


I have this query, whoch runs fine when I test it in Access, but I'm running it from TSQL in an outside C# program (honestly, I'm still learning the terms so I think TSQL is correct for what I'm doing (It was not)). When I try to store the procedure in a tableadapter in my XSD file I get an error (see below the query). As a result the proper XML schema is not automatically created. While the query still runs it also gives an error. I'd like it to run without the error but can't figure out where that error really is. I tried eliminating all the "=" by replacing the IIFs with CASE statements but then I got other errors ( I can post that attempt if it would be helpful)

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 message from Query Builder

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

Anyway, thanks for the help!

EDIT:

This was my previous attempt at replacing the IIFs with CASEs

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]

And the error I get is

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

EDIT2:

So this query works just fine

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)

But as soon as I add any Sum (be it through CASEs or IIFs) the query generates an error.


回答1:


You can use a COALESCE to replace the NULL columns with an empty string and a CASE to replace the IIF in the SUMs.

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



回答2:


You need to convert your IIF statements to CASE statements, IIF is not valid T-SQL syntax.

In response to your comment, that's not how a null check is done in t-sql, here is an example:

CASE
     WHEN SomeValue IS NULL THEN
          SomeDefault
     ELSE
          SomeValue 

END



回答3:


A lot of syntax-errors.

I formated the last attempt you made and corrected some errors. Hope this works for you.

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


来源:https://stackoverflow.com/questions/8563231/query-runs-in-access-but-generates-error-in-c-sharp-dataset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!