2014-01-23 101 views
0

我遇到了一個我繼承的程序的問題。我需要調整查詢,但是查詢不起作用。SQL Server 2008:關鍵字'AS'附近的語法不正確

這是我得到的查詢(不審判我,這不是我的查詢:))

SELECT  
    SUM(DATEDIFF(day, verz1.BeginDatumAW, verz1.EindDatumAW) + 1) AS AantalDagen,  
    verz1.CodeBedrijf, verz1.CodeAfdeling, verz1.CodeSectie, 
    (SELECT TOP (1) RTRIM(OmschrijvingSectie) AS Expr1 
    FROM PEBSectie 
    WHERE (CodeBusiness = verz1.CodeBusiness) AND (CodeAfdeling = '00')) AS BSNS, 
    (SELECT TOP (1) RTRIM(OmschrijvingSectie) AS Expr1 
    FROM PEBSectie AS PEBSectie_1 
    WHERE (CodeBusiness = verz1.CodeBusiness) AND (CodeAfdeling = verz1.CodeAfdeling) AND (CodeGroep = '00')) AS AFD, 
    (SELECT TOP (1) RTRIM(OmschrijvingSectie) AS Expr1 
    FROM PEBSectie AS PEBSectie_1 
    WHERE (CodeBusiness = verz1.CodeBusiness) AND (CodeAfdeling = verz1.CodeAfdeling) AND (CodeGroep = verz1.CodeGroep) AND (CodeSectie = '00')) AS GRP, 
    (SELECT TOP (1) RTRIM(OmschrijvingSectie) AS Expr1 
    FROM PEBSectie AS PEBSectie_1 
    WHERE (CodeBusiness = verz1.CodeBusiness) AND (CodeAfdeling = verz1.CodeAfdeling) AND (CodeGroep = verz1.CodeGroep) AND (CodeSectie = verz1.CodeSectie)) AS SEC 
FROM   
    Verzorging AS verz1 
LEFT OUTER JOIN 
    AangepastWerk AS aw1 ON verz1.VerzorgingId = aw1.VerzorgingId 
WHERE  
    (verz1.EindDatumAW <= @EndDate) AND 
    (verz1.BeginDatumAW >= @StartDate) AND 
    (aw1.Verwijderd = 0) AND    <------------ First statement added 
    (aw1.NietErkend = 0 OR aw1.NietErkend IS NULL) AND 
    (DATEDIFF(day, verz1.BeginDatumAW, verz1.EindDatumAW) > 0)) 
GROUP BY 
    verz1.CodeBusiness, verz1.CodeBedrijf, verz1.CodeAfdeling, verz1.CodeGroep, 
    verz1.CodeSectie, verz1.TypeAWOfWO, verz1.TypeOngeval 
HAVING  
    (verz1.TypeAWOfWO = 'A') AND (verz1.TypeOngeval = 'AO') OR 
    (verz1.TypeAWOfWO = 'Arbeidsongeval') OR 
    (verz1.TypeAWOfWO = 'arbeidsongeval') 
UNION ALL 
SELECT  SUM(DATEDIFF(day, @StartDate, verz1.EindDatumAW) + 1) AS AantalDagen, verz1.CodeBedrijf, verz1.CodeAfdeling, verz1.CodeSectie, 
         (SELECT  TOP (1) RTRIM(OmschrijvingSectie) AS Expr1 
         FROM   PEBSectie AS PEBSectie_3 
         WHERE  (CodeBusiness = verz1.CodeBusiness) AND (CodeAfdeling = '00')) AS BSNS, 
         (SELECT  TOP (1) RTRIM(OmschrijvingSectie) AS Expr1 
         FROM   PEBSectie AS PEBSectie_1 
         WHERE  (CodeBusiness = verz1.CodeBusiness) AND (CodeAfdeling = verz1.CodeAfdeling) AND (CodeGroep = '00')) AS AFD, 
         (SELECT  TOP (1) RTRIM(OmschrijvingSectie) AS Expr1 
         FROM   PEBSectie AS PEBSectie_1 
         WHERE  (CodeBusiness = verz1.CodeBusiness) AND (CodeAfdeling = verz1.CodeAfdeling) AND (CodeGroep = verz1.CodeGroep) AND 
               (CodeSectie = '00')) AS GRP, 
         (SELECT  TOP (1) RTRIM(OmschrijvingSectie) AS Expr1 
         FROM   PEBSectie AS PEBSectie_1 
         WHERE  (CodeBusiness = verz1.CodeBusiness) AND (CodeAfdeling = verz1.CodeAfdeling) AND (CodeGroep = verz1.CodeGroep) AND 
               (CodeSectie = verz1.CodeSectie)) AS SEC 
FROM   Verzorging AS verz1 LEFT OUTER JOIN 
        AangepastWerk AS aw1 ON verz1.VerzorgingId = aw1.VerzorgingId 
WHERE  (verz1.EindDatumAW <= @EndDate) 
AND (verz1.EindDatumAW >= @StartDate) 
AND (verz1.BeginDatumAW < @StartDate) 
AND (aw1.Verwijderd = 0)     <------------ Second statement added 
AND (aw1.NietErkend = 0 OR aw1.NietErkend IS NULL) 
AND (DATEDIFF(day, verz1.BeginDatumAW, verz1.EindDatumAW) > 0) 
GROUP BY verz1.CodeBusiness, verz1.CodeBedrijf, verz1.CodeAfdeling, verz1.CodeGroep, verz1.CodeSectie, verz1.TypeAWOfWO, verz1.TypeOngeval 
HAVING  (verz1.TypeAWOfWO = 'A') AND (verz1.TypeOngeval = 'AO') OR 
        (verz1.TypeAWOfWO = 'Arbeidsongeval') OR 
        (verz1.TypeAWOfWO = 'arbeidsongeval') 

變化:在兩種情況報表添加以下行:

AND (aw1.Verwijderd = 0) 

我添加這個後,我不斷收到以下子查詢的問題(對於每個子查詢,我得到相同的錯誤)。

(SELECT  TOP (1) RTRIM(OmschrijvingSectie) AS Expr1 
         FROM   PEBSectie AS PEBSectie_3 
         WHERE  (CodeBusiness = verz1.CodeBusiness) AND (CodeAfdeling = '00')) AS BSNS, 

我得到的錯誤是:

消息156,級別15,狀態1,行 'AS' 關鍵字近28
不正確的語法。

我似乎無法弄清楚我做了什麼/出了什麼問題。我希望一雙清新的眼睛可能會看到我錯過的東西。

注意:後UNION ALL

感謝您對我所有的耐心的錯誤只發生

與親切的問候

編輯:如果可能的話:大要避免修改查詢。

+0

最後..荷蘭人字段名。 :) – NickyvV

+0

你究竟在哪裏添加了行'AND(aw1.Verwijderd = 0)'? – NickyvV

+0

進入兩個主要select語句的where語句。將嘗試編輯我的帖子,讓他們跳出來。 @Marc_s感謝編輯!現在更可讀! – User999999

回答

0

大家好,

感謝您所有的時間投入到我的問題。一雙新鮮的眼睛(即使是一對非IT眼睛)突然發現了這個問題。在第一個查詢中有一個支架太多(其中語句)

(DATEDIFF(day, verz1.BeginDatumAW, verz1.EindDatumAW) > 0)) 

最後一個支架不應該在那裏。我被錯誤消息所矇蔽,並在第二個查詢中查找問題。舊的查詢如何仍然能夠產生有效的結果(即使語法無效)仍然讓我感到困惑。查詢本身將被重寫以避免更多問題。

日Thnx再次所有的時間

隨着親切的問候

0

你不能別名選擇表查詢..

(SELECT TOP (1) RTRIM(OmschrijvingSectie) AS Expr1 
FROM PEBSectie AS PEBSectie_1 
WHERE (CodeBusiness = verz1.CodeBusiness) AND 
     (CodeAfdeling =verz1.CodeAfdeling) AND 
     (CodeGroep = verz1.CodeGroep) AND          
     (CodeSectie = verz1.CodeSectie)) **AS SEC** 

是錯在這裏對所有表走樣。

+1

這不是事實。儘管別名已經在第一個SELECT中設置(在UNION ALL之前),但您也可以別名其他列,但SQL對它們沒有任何作用。看到這個演示:http://sqlfiddle.com/#!6/d41d8/14033 – NickyvV

相關問題