The SELECT item identified by the ORDER BY number %d contains a variable as part of the expression identifying a column position. Variables are only allowed when ordering by an expression referencing a column name.

Error Message:
Msg 1008, Level 16, State 1, Line 10
The SELECT item identified by the ORDER BY number %d contains a variable as part of the expression identifying a column position. Variables are only allowed when ordering by an expression referencing a column name.

Severity level:
16.

Description:
This error message appears when you try to use a variable in the ORDER BY clause that identifies a column position instead of a column name.

Consequences:
The T-SQL statement can be parsed, but causes the error at runtime.

Resolution:
Error of the Severity level 15 are generated by the user and can be fixed by the SQL Server user. The statement cannot be run this way. You can not use a variable in the ORDER BY clause that identifies a column by its ordinal position.

Versions:
All versions of SQL Server.

Example(s):
IF OBJECT_ID(‘dbo.t’) IS NOT NULL
    DROP TABLE dbo.t;
GO

CREATE TABLE dbo.t
(
    c1 int
);
GO

DECLARE @i int;

SET @i = 1
SELECT
    MAX(c1)
FROM
    dbo.t
ORDER BY
    @i;

Remarks:
In the above example we try to run a query that uses a variable in the ORDER BY and refers to the item in the SELECT list by its ordinal position. This raises the error.




Array

No comments yet... Be the first to leave a reply!