March 25, 2014 at 5:29 am
We came across a "gotcha" while testing our application without being in 2000 compatibility mode. We fixed all the *= left join syntax, but in another stored procedure got the error:
[font="Courier New"]ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.[/font]
The problem is that in our SELECT statement we convert a date to varchar, but then also want to use that column in the ORDER BY. But since it is not included in the select as a standalone field, we get the error.
SELECT DISTINCT
a.JOB_ID,
a.Fld2,
a.Fld3,
CONVERT(datetime, CONVERT(varchar(10), a.JOB_Date, 101)) AS JOB_Date,
UNION
SELECT DISTINCT
a.JOB_ID,
a.Fld2,
a.Fld3,
CONVERT(datetime, CONVERT(varchar(10), a.JOB_Date, 101)) AS JOB_Date,
ORDER BY a.JOB_Date
If I remove the alias from Job_Date in the order by, then it works in 2008 mode
Any ideas how to search for similar situations ??
I suppose I can scan all our code for: UNION, INTERSECT or EXCEPT
March 25, 2014 at 5:52 am
Try 'ORDER BY JOB_Date' not 'ORDER BY a.JOB_Date'.
The query should then be order by the JOB_Date expression.
March 25, 2014 at 6:23 am
Any luck with UPGRADE ADVISOR?
---------------------------------------------------
"Thare are only 10 types of people in the world:
Those who understand binary, and those who don't."
March 25, 2014 at 7:14 am
Yes, removing the alias works, but it seems strange that it is not valid code. Upgrade Advisor won't work because we have Cold Fusion and PHP code outside of SQL.
March 25, 2014 at 9:27 am
The reason that makes the query work without the alias is that a.JOB_Date is different to JOB_Date which is the column alias.
I'm not sure how would you be able to look for this problems. A third party tool might work, but I don't work with any.
March 25, 2014 at 10:20 am
homebrew01 (3/25/2014)
We came across a "gotcha" while testing our application without being in 2000 compatibility mode. We fixed all the *= left join syntax, but in another stored procedure got the error:[font="Courier New"]ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.[/font]
The problem is that in our SELECT statement we convert a date to varchar, but then also want to use that column in the ORDER BY. But since it is not included in the select as a standalone field, we get the error.
SELECT DISTINCT
a.JOB_ID,
a.Fld2,
a.Fld3,
CONVERT(datetime, CONVERT(varchar(10), a.JOB_Date, 101)) AS JOB_Date,
UNION
SELECT DISTINCT
a.JOB_ID,
a.Fld2,
a.Fld3,
CONVERT(datetime, CONVERT(varchar(10), a.JOB_Date, 101)) AS JOB_Date,
ORDER BY a.JOB_Date
If I remove the alias from Job_Date in the order by, then it works in 2008 mode
Any ideas how to search for similar situations ??
I suppose I can scan all our code for: UNION, INTERSECT or EXCEPT
You couldn't use a.JOB_DATE in the order by clause because it isn't a column in the table aliased by a, it is a column alias for the CONVERT function in your select list.
March 25, 2014 at 10:40 am
My snippet is not very clear.
Job_Date is a DATETIME column in the table, and it's also getting named as a field as a result of the convert. So a.Job_Date exists, but not listed as part of the SELECT clause, so I guess in 2008, a UNION needs the columns selected if they are going to be used in the ORDER BY ??
The code works in 2000, but not 2008.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply