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 are corrigible by the 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.



Related Articles :

  • No Related Articles Found

One Response to “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.”

  1. DECLARE @orderCol varchar(50)
    set @orderCol=’dep_Name’
    select * from Department_Table order by CONVERT(varchar(20), @orderCol)

Software Reviews | Book Reviews | FAQs | Tips | Articles | Performance Tuning | Audit | BI | Clustering | Developer | Reporting | DBA | ASP.NET Ado | Views tips | | Developer FAQs | Replication Tips | OS Tips | Misc Tips | Index Tuning Tips | Hints Tips | High Availability Tips | Hardware Tips | ETL Tips | Components Tips | Configuration Tips | App Dev Tips | OLAP Tips | Admin Tips | Software Reviews | Error | Clustering FAQs | Performance Tuning FAQs | DBA FAQs |