除非还指定了TOP或FOR XML,否则ORDER BY子句无效

时间:2010-03-16 12:19:15

标签: sql-server asp-classic

我得到“ORDER BY子句在视图,内联函数,派生表,子查询和公用表表达式中无效,除非还指定了TOP或FOR XML。”以下代码出错。我最初有两张桌子,ADSAREAS&类别。当我删除CATEGORIES表时,我开始收到此错误。

    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)

5 个答案:

答案 0 :(得分:11)

您在重写时将ORDER BY子句移动到内部查询。在WHERE子句后添加括号(和标识符),以便ORDER BY应用于外部SELECT。

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

答案 1 :(得分:7)

替代已接受的答案,您只需使用TOP (100) PERCENT

即可

例如:

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 

现在您的ORDER将正常工作

答案 2 :(得分:2)

如果您查看代码生成的查询,将会有所帮助:

SELECT * 
FROM (
  SELECT 
    A.AdsAreasID, 
    A.AdsAreasName, 
    ROW_NUMBER() OVER (ORDER BY A.AdsAreasID) As Row
  FROM ADSAREAS A
  WHERE 
    Row > ("& RecordsPageSize - RecordsPerPage &") 
    AND Row <= ("& RecordsPageSize &")
  ORDER BY" & SQLCONT1 & SQLCONT2

请注意,您错过了一个结束)字符,并且如果一个字符放在最后,ORDER BY将位于内部查询内,而不是外部查询,这是无效的。你最好完全删除外部查询。它没有用处。

尽管SQL注入涉及以这种方式构建查询,但这仍然存在。

答案 3 :(得分:2)

除了上面接受的答案之外,还可以从 Inside Microsoft SQL Server 2008:T-SQL编程一书第1章第4页说明:

  

请注意,错误并不是说完全不允许ORDER BY;相反,它表示允许的几个例外 - 当还指定了TOP或FOR XML时。请记住,TOP和FOR XML都是T-SQL扩展,而不是标准SQL元素。 TOP和ORDER BY或ORDER BY和FOR XML是结果集规范的一部分,其中ORDER BY本身不是,并且仅指定表示的细节。因此,在视图定义中允许使用TOP和ORDER BY或ORDER BY和FOR XML,而单独的ORDER BY则不允许。

答案 4 :(得分:0)

看起来问题是您正在尝试将order by子句应用于禁止的内部select语句。您应该只将order by子句应用于最外面的select语句。