2011-12-19 40 views
1

我有這個查詢,當我在Access中測試時,whoch運行良好,但我在外部C#程序中運行它 來自TSQL (老實說,學習術語,所以我認爲TSQL對於我正在做的事情是正確的(不是))。當我嘗試將過程存儲在我的XSD文件的tableadapter中時,出現錯誤(請參閱下面的查詢)。因此,不會自動創建正確的XML模式。雖然查詢仍然運行它也給出了一個錯誤。我希望它能夠在沒有錯誤的情況下運行,但無法弄清楚錯誤究竟在哪裏。我試圖消除所有的「=」用CASE語句替換綜合投資框架,但後來我得到其他錯誤從查詢生成器查詢在Access中運行,但在C#數據集中生成錯誤

Error in list of function arguments: '=' not recognized. 
Unable to parse query text. 
(我可以張貼的嘗試是否會有所幫助)

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) 

錯誤信息

無論如何,感謝您的幫助!

編輯:

這是我以前在案件

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) 

但只要我添加任何款項(無論是通過案件或綜合投資框架)查詢產生一個錯誤。

回答

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 
+0

爲什麼查詢設計者想改變'COALESCE(Owners.OwnFirstName + '', '')''到[COALESCE(Owners.OwnFirstName + '', '')'? – Brad 2011-12-19 15:21:06

+0

@Brad不知道。 – 2011-12-19 15:23:22

+0

當我嘗試運行你在那裏的東西時,我在查詢表達式SUM(CASE WHEN UnitBarcodes.NephStatus ='N'THEN 1 ELSE 0 END)'中得到'語法錯誤(缺少運算符)',然後突出顯示'WHEN'短語。 (這是當試圖在Access中,我也得到一個錯誤,只是di9fferent,在VS'IErrorInfo.Getdescription失敗,E_FAIL(0x80004005)。) – Brad 2011-12-19 15:27:42

0

您需要將您的IIF語句轉換爲CASE語句,IIF不是有效的T-SQL語法。

在回答您的意見,這是一個空檢查也不怎麼在T-SQL完成的,這裏有一個例子:

CASE 
    WHEN SomeValue IS NULL THEN 
      SomeDefault 
    ELSE 
      SomeValue 

END 
+0

看我上面的編輯。 – Brad 2011-12-19 15:16:26

+0

@Maess讓我來向您介紹[COALESCE](http://msdn.microsoft.com/en-us/library/ms190349.aspx)和[ISNULL](http://msdn.microsoft.com/zh-cn/ us/library/ms184325.aspx)函數。 ;-) – 2011-12-19 15:27:02

+0

嗨,喬,我知道COALESCE,但是,你需要從基本面開始,像CASE和如何做空檢查。是的,它有一個功能,但最好從它的實際工作開始。 – Maess 2011-12-19 15:35:55

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 
+0

我試過這個,但它仍然給出錯誤。我試圖把它分解成更小的部分。 'SUM(當UnitBarcodes.NephStatus ='N'時,則爲1,否則爲0結束)AS [Neph單位]和'SELECT SUM(iif(MicroPEMUnitBarcodes.NephStatus ='N',1,0))AS [Neph單位] FROM MicroPEMUnitBarcodes'都會返回錯誤,但錯誤發生後的第二個錯誤會以我想要的方式返回我的數據。 (這兩個錯誤都是'語法錯誤') – Brad 2011-12-19 16:25:28

+0

那麼,我必須放棄這一個。現在看來,這個問題不考慮t-sql。我和Joe Stefanelli一樣誤解了這個問題。我無法幫你解決這個問題。抱歉! – Johan 2011-12-20 09:33:00

+0

:/對不起。好吧。我將繼續嘗試與此合作。感謝您的幫助。 – Brad 2011-12-20 12:10:33