The ORDER BY position number %ld is out of range of the number of items in the select list.

Error Message:
Server: Msg 108, Level 15, State 1, Line n
The ORDER BY position number x is out of range of the number of items in the select list.

Severity level:
15.

Description:
SQL Server allows to reference a column in the ORDER BY clause by its ordinal position in the final resultset. This message appears when you specify a position number in the ORDER BY clause that doesn’t have a corresponding column in the SELECT list. For illustration purposes see the examples below.

Consequences:
The SQL statement cannot be parsed and further execution is stopped.

Resolution:
Error of the severity level 15 are generated by the user and can be fixed by the SQL Server user.

Versions:
All versions of SQL Server.

Example(s):
SELECT OrderID
  FROM Northwind..Orders
 ORDER BY 0

SELECT c1
  FROM
   (SELECT 1 UNION ALL
    SELECT 2 UNION ALL
    SELECT 3) AS t (c1)
 ORDER BY 2

Remarks:
The first statement is invalid because there is no column position 0 in the SELECT list. Likewise the second query is invalid, because there is no position number 2 in the SELECT list. For both queries an ORDER BY 1 would make the statements valid and executable.

]]>

Leave a comment

Your email address will not be published.