[英]SQL Server 2008: Incorrect syntax near the keyword 'AS'
I have a problem with a program I inherited. 我继承的程序有问题。 I needed to adjust a query but nog the query doesn't work anymore.
我需要调整查询,但查询不再起作用。
This is the query I got (don't judge me, it isn't my query :) ) 这是我得到的查询(不要判断我,这不是我的查询:))
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')
Changes: In both where statements I added the following line: 更改:在两个where语句中,我都添加了以下行:
AND (aw1.Verwijderd = 0)
After i added this i keep getting the problems with the following subqueries (for each of the subqueries i get the same error). 添加完之后,我继续遇到以下子查询的问题(对于每个子查询,我都会遇到相同的错误)。
(SELECT TOP (1) RTRIM(OmschrijvingSectie) AS Expr1
FROM PEBSectie AS PEBSectie_3
WHERE (CodeBusiness = verz1.CodeBusiness) AND (CodeAfdeling = '00')) AS BSNS,
The error I get is: 我得到的错误是:
Msg 156, Level 15, State 1, Line 28
消息156,第15层,州1,第28行
Incorrect syntax near the keyword 'AS'.关键字“ AS”附近的语法不正确。
I can't seem to figure out what I've done/ what has gone wrong. 我似乎无法弄清楚我做了什么/出了什么问题。 I hoped a pair of fresh eyes might see something I've missed.
我希望一双新鲜的眼睛可能会看到我错过的东西。
Note : The error only occurs after the UNION ALL 注意 :该错误仅在UNION ALL之后发生
I thank you for all your patience 感谢您的耐心配合
with kind regards 亲切的问候
Edit: if possible: major Modifications to the query are to be avoided. 编辑:如果可能的话:要避免对该查询进行重大修改。
You cannot alias for the select table query.. 您不能为选择表查询别名。
(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**
is wrong here for all the table aliasing. 对于所有表别名,这里都是错误的。
Everyone, 大家,
Thanks for all the time you guys invested into my problem. 感谢您一直以来为我的问题投入的精力。 A fresh pair of eyes (even a pair of non IT-Eyes ;) ) suddenly spotted the problem.
一双新鲜的眼睛(甚至是一双非IT眼睛的眼睛)都突然发现了问题所在。 There is one bracket too much in the first query (where statement)
第一个查询(where语句)中的括号过多
(DATEDIFF(day, verz1.BeginDatumAW, verz1.EindDatumAW) > 0))
That last bracket shouldn't be there. 最后一个括号不应该在那里。 I was blinded by the errormessage and was looking for the problem in the second query.
我对错误消息视而不见,并在第二个查询中寻找问题。 How the old query still managed to produce valid results (even with an invalid syntax) still baffles me.
旧的查询如何仍然设法产生有效的结果(即使使用无效的语法)仍然让我感到困惑。 The query itself will be rewritten to avoid further problems.
查询本身将被重写以避免进一步的问题。
Thnx again for all your time 再次感谢您
With kind regards 亲切的问候
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.