I get "The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified." error with the following code. I initially had two tables, ADSAREAS & CATEGORIES. I started receiving this error when I removed CATEGORIES table.
Select Case SIDX
Case "ID" : SQLCONT1 = " AdsAreasID"
Case "Page" : SQLCONT1 = " AdsAreasName"
Case Else : SQLCONT1 = " AdsAreasID"
End Select
Select Case SORD
Case "asc" : SQLCONT2 = " ASC"
Case "desc" : SQLCONT2 = " DESC"
Case Else : SQLCONT2 = " ASC"
End Select
''# search feature --->
Select Case SEARCHFIELD
Case "ID" : SQLSFIELD = "AND AdsAreasID"
Case "Ads Areas" : SQLSFIELD = "AND AdsAreasName"
Case Else : SQLSFIELD = ""
End Select
Select Case SEARCHOPER
Case "eq" : SQLSOPER = " = " & SEARCHSTRING
Case "ne" : SQLSOPER = " <> " & SEARCHSTRING
Case "lt" : SQLSOPER = " <" & SEARCHSTRING
Case "le" : SQLSOPER = " <= " & SEARCHSTRING
Case "gt" : SQLSOPER = " >" & SEARCHSTRING
Case "ge" : SQLSOPER = " >= " & SEARCHSTRING
Case "bw" : SQLSOPER = " LIKE '" & SEARCHSTRING & "%' "
Case "ew" : SQLSOPER = " LIKE '%" & SEARCHSTRING & "' "
Case "cn" : SQLSOPER = " LIKE '%" & SEARCHSTRING & "%' "
Case Else : SQLSOPER = ""
End Select
''# search feature --->
SQL = "SELECT * FROM ( SELECT A.AdsAreasID, A.AdsAreasName, ROW_NUMBER() OVER (ORDER BY A.AdsAreasID) As Row"
SQL = SQL & " FROM ADSAREAS A"
SQL = SQL & " WHERE Row > ("& RecordsPageSize - RecordsPerPage &") AND Row <= ("& RecordsPageSize &") ORDER BY" & SQLCONT1 & SQLCONT2
Set objXML = objConn.Execute(SQL)
"The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified."
This error will be encountered if you add a Sort Type to the SQL Query Builder without changing how the Query works. You will need to instead change the Query to select the Top 100% Grouped by All, and change the Group By on all Fields - this will allow you to export.
An ORDER BY command cannot be used in a subquery, although the main query can use an ORDER BY. The GROUP BY command can be used to perform the same function as the ORDER BY in a subquery. Subqueries that return more than one row can only be used with multiple value operators such as the IN operator.
The ORDER BY clause is not valid in views, inline functions, derived tables, and subqueries, unless either the TOP or OFFSET and FETCH clauses are also specified.
You moved the ORDER BY clause to the inner query in rewriting it. Add a parentheses (and identifier) after the WHERE clause so that ORDER BY applies to the outer SELECT instead.
SQL = "SELECT * FROM ( SELECT A.AdsAreasID, A.AdsAreasName, ROW_NUMBER() OVER (ORDER BY A.AdsAreasID) As Row"
SQL = SQL & " FROM ADSAREAS A"
SQL = SQL & " WHERE Row > ("& RecordsPageSize - RecordsPerPage &") AND Row <= ("& RecordsPageSize &")) inner ORDER BY" & SQLCONT1 & SQLCONT2
Alternative to accepted answer you can simply use TOP (100) PERCENT
for example:
SELECT table1Col, ...
FROM yourTABLE1
JOIN -- doesn't matter what join you use
( SELECT TOP (100) PERCENT
table2Col, ...
FROM yourTABLE2
ORDER BY table2Col,....
) AS TB2 ON yourTABLE1.Col = TB2.Col
now your ORDER
will work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With