我遇到了一個我繼承的程序的問題。我需要調整查詢,但是查詢不起作用。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
感謝您對我所有的耐心的錯誤只發生
與親切的問候
編輯:如果可能的話:大要避免修改查詢。
最後..荷蘭人字段名。 :) – NickyvV
你究竟在哪裏添加了行'AND(aw1.Verwijderd = 0)'? – NickyvV
進入兩個主要select語句的where語句。將嘗試編輯我的帖子,讓他們跳出來。 @Marc_s感謝編輯!現在更可讀! – User999999