关键字' DESC'附近的语法不正确

时间:2014-05-18 13:30:35

标签: sql sql-server sql-order-by

我正在尝试编写SQL查询,但是当我尝试保存它时会抛出错误的语法错误。 这是查询:

(
@top int,
@bottom int
)

AS
        SELECT *
        FROM (SELECT top (@bottom - @top + 1) *
            FROM (SELECT top (@bottom) *
                  FROM    tblPlayers INNER JOIN
                          tblPosition ON tblPlayers.IDPosition = tblPosition.IDPosition INNER JOIN
                          tblTeams ON tblPlayers.IDTeam = tblTeams.IDTeam
                  ORDER BY (PPG DESC, APG DESC, RPG DESC)
                 )
            ORDER BY PPG ASC, APG ASC, RPG ASC
             )      
        ORDER BY PPG DESC, APG DESC, RPG DESC
    RETURN

问题在于最内在的ORDER BY。如果没有它的括号,它会在关键字“ORDER”附近给出不正确的语法。使用括号,错误是关键字'DESC'附近的语法不正确。 我没有想法。语法对我来说似乎很好。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

您的查询需要表别名,这可能是问题的一部分或全部。此外,您应该在最里面的子查询中明确列出列,因为有重复的名称:

    SELECT t.*
    FROM (SELECT top (@bottom - @top + 1) t.*
        FROM (SELECT top (@bottom) co1, col2, col3, . . .
              FROM    tblPlayers INNER JOIN
                      tblPosition ON tblPlayers.IDPosition = tblPosition.IDPosition INNER JOIN
                      tblTeams ON tblPlayers.IDTeam = tblTeams.IDTeam
              ORDER BY PPG DESC, APG DESC, RPG DESC
             ) t
        ORDER BY PPG ASC, APG ASC, RPG ASC
         ) t   
    ORDER BY PPG DESC, APG DESC, RPG DESC