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:
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
Incorrect syntax near the keyword '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
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. There is one bracket too much in the first query (where statement)
(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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.